QComboBox中QAbstractItemView寬度設置

在開發時遇到需要將QComboBox顯示Item的View寬度增加,使其寬於QComboBox本身,否則Item中的文本顯示不全。

設置方法有兩種,一是使用styleSheet,而是直接訪問QAbstractItemView設置。

詳細請參見代碼:

#pragma once

#include <QtWidgets/QComboBox>

class QComboBoxEx : public QComboBox
{
    Q_OBJECT

public:
    QComboBoxEx(QWidget *parent = Q_NULLPTR);
	~QComboBoxEx(){};
	void initUi(bool bStyleSheet);
};

#include "QComboBoxEx.h"
#include <QAbstractItemView>

QComboBoxEx::QComboBoxEx(QWidget *parent)
	: QComboBox(parent)
{
	
}

void QComboBoxEx::initUi(bool bStyleSheet)
{
	if (bStyleSheet)
	{
		setStyleSheet(QString("QComboBox{height:20px;width:200px}"
			"QAbstractItemView{min-width:400px;height:200px}"));
	}
	else
	{
		const_cast<QAbstractItemView*>(this->view())->setMinimumWidth(400);
	}
	QStringList list;
	list << QString("Item1") << QString("Item2") << QString("Item3") << QString("Item4") << QString("Item5");
	addItems(list);
}
#include "QComboBoxEx.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QComboBoxEx w;
	w.initUi(false);
    w.show();
    return a.exec();
}

交流q:1245178753
本文地址:http://blog.csdn.net/u011417605/article/details/61922669
發佈了119 篇原創文章 · 獲贊 91 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章