Qt 自定义Combo Box - Windows Live

Qt 自定义 Combo Box

这星期效率还不错,延续了上周的状态,写了非常多的代码,但是大部分都是低复杂度的”简单代码”,写了一些Gui。和一些同事不一样,我很喜欢写Gui,因为写完马上就可以看到成果,很有成就感,而且用Qt写Gui感觉就像堆积木,很简单,只要找到自己需要的API,我个人非常喜欢Qt风格的C++的应用程序接口,很清晰且简单,相当直觉化

这里和大家很简单谈一些我最近用Qt写Gui的发现的好玩特性,也许Qt的标准控件已经不能满足你了,其实,我们同样可以很容易地进行自定义控件,这里使用Combo Box作为例子。

以下代码会构建出我们自定义的Combo Box,如附图所示,这里使用了。

?

?

//?set?some?self?defined?colors
QColor?royalBlue;
QColor?orange;
royalBlue.setRgb(65,?105,?225);
orange.setRgb(255,?165,?000);
//?set?default?color
myComboBox->setStyleSheet("*?{?background-color:?rgb(65,?105,?225)?}");
myComboBox->insertItem(0,?"blue");
myComboBox->setItemData(0,?royalBlue,?Qt::BackgroundRole);
myComboBox->insertItem(1,?"red");
myComboBox->setItemData(1,?Qt::red,?Qt::BackgroundRole);
myComboBox->insertItem(2,?"green");
myComboBox->setItemData(2,?Qt::green,?Qt::BackgroundRole);
myComboBox->insertItem(3,?"yellow");
myComboBox->setItemData(3,?Qt::yellow,?Qt::BackgroundRole);
myComboBox->insertItem(4,?"orange");
myComboBox->setItemData(4,?orange,?Qt::BackgroundRole);
myComboBox->insertItem(5,?"cyan");
myComboBox->setItemData(5,?Qt::cyan,?Qt::BackgroundRole);
myComboBox->insertItem(6,?"purple");
myComboBox->setItemData(6,?Qt::magenta,?Qt::BackgroundRole);

?

?

也许还不过瘾,我们还可以通过来自定义我们的Combo Box,以下是一个简单例子。

QPalette?myPalette?=?myComboBox->palette();
//?outline?around?the?menu
myPalette.setColor(QPalette::Window,?Qt::red);
myPalette.setColor(QPalette::WindowText,?Qt::white);
//?customize?myComboBox?button
myPalette.setColor(QPalette::Button,?Qt::darkCyan);
myPalette.setColor(QPalette::ButtonText,?Qt::white);
//?customize?myComboBox?menu
myPalette.setColor(QPalette::Base,?Qt::darkCyan);
myPalette.setColor(QPalette::Text,?Qt::white);
//?customize?highlight?button?and?menu
myPalette.setColor(QPalette::Highlight,?Qt::blue);
myPalette.setColor(QPalette::HighlightedText,?Qt::red);
//?customize?the?disabled?color
myPalette.setColor(QPalette::Disabled,?QPalette::Button,?Qt::gray);
myPalette.setColor(QPalette::Disabled,?QPalette::ButtonText,?Qt::lightGray);
myComboBox->setPalette(myPalette);

郑重声明:资讯 【Qt 自定义Combo Box - Windows Live】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——