简体   繁体   English

Qt编程QComboBox

[英]Qt programming QComboBox

I am using Qt and C++ to add some features to a freeware called: EASYPAINT . 我正在使用QtC++向名为EASYPAINT的免费软件中添加一些功能。

I had to add a more intuitive method in which the users can see the actual width directly from the tool instead of changing numbers. 我必须添加一种更直观的方法,使用户可以直接从工具中看到实际宽度,而无需更改数字。 ( just like in the new windows paint , where you can actually see the line thickness and not pixels.) (就像在新的Windows paint中一样,您实际上可以看到线的粗细而不是像素。)

I am using a QComboBox . 我正在使用QComboBox My question is (look at the code first), instead of having 20 ( penSizeList->addItem ), I know we can have addItems .... But what about the icon. 我的问题是(先看看代码),而不是拥有20个( penSizeList->addItem ),我知道我们可以拥有addItems ....但是图标呢? for each Item , will I have to search for 20 different line thickness.png and add them? 对于每个Item ,我是否必须搜索20种不同的line thickness.png并将其添加? Or is there another method I can use? 还是我可以使用另一种方法?

And also, how can I get rid of the string in addItem , and only keep an image or icon in the QComboBox . 而且,我如何摆脱addItem中的字符串,而仅将图像或图标保留在QComboBox

QComboBox *penSizeList = new QComboBox();
penSizeList->setIconSize(QSize(100,100));
penSizeList->setStatusTip("Pen Size");

QIcon ONEpxIcon(":/media/actions-icons/clear-gray.png");
QIcon THREEpxIcon(":/media/instruments-icons/canvas-lines1.png");

penSizeList->addItem(ONEpxIcon,"1px");
penSizeList->addItem(THREEpxIcon,"2px");
penSizeList->addItem(THREEpxIcon,"3px");
penSizeList->addItem(THREEpxIcon,"4px");
penSizeList->addItem(THREEpxIcon,"5px");
penSizeList->addItem(THREEpxIcon,"6px");
penSizeList->addItem(THREEpxIcon,"7px");
penSizeList->addItem(THREEpxIcon,"8px");
penSizeList->addItem(THREEpxIcon,"9px");
penSizeList->addItem(THREEpxIcon,"10px");
penSizeList->addItem(THREEpxIcon,"11px");
penSizeList->addItem(THREEpxIcon,"12px");
penSizeList->addItem(THREEpxIcon,"13px");
penSizeList->addItem(THREEpxIcon,"14px");
penSizeList->addItem(THREEpxIcon,"15px");
penSizeList->addItem(THREEpxIcon,"16px");
penSizeList->addItem(THREEpxIcon,"17px");
penSizeList->addItem(THREEpxIcon,"18px");
penSizeList->addItem(THREEpxIcon,"19px");
penSizeList->addItem(THREEpxIcon,"20px");

connect(penSizeList,SIGNAL(activated(int)), this, SLOT(penValueChanged(int)));

Try to do this in loop. 尝试循环执行此操作。

penSizeList->addItem(ONEpxIcon,"1px");
for(int i = 2; i < 21 ; i++)
{
    penSizeList->addItem(THREEpxIcon,QString("%1px").arg(i));
}

Or if you have different icons for each line: 或者,如果每行都有不同的图标:

for(int i = 1; i < 21 ; i++)
{
    penSizeList->addItem(QIcon(QString("iconLine%1.png").arg(i)),QString("%1px").arg(i));
}

If you want only icons, set empty string: 如果只需要图标,请设置空字符串:

penSizeList->addItem(icon,"");

If you want full image then you should set this image as background. 如果要获得完整图像,则应将此图像设置为背景。 For example: 例如:

QPixmap pxmap("G:/2/qt.jpg");
QStandardItemModel *md = new QStandardItemModel;
QStandardItem *iii = new QStandardItem;
iii->setBackground(QBrush(Qt::red));
iii->setText("ss");
QStandardItem *iiii = new QStandardItem;
iiii->setBackground(QBrush(pxmap));
iiii->setText("ss");
md->setItem(1,0,iii);
md->setItem(0,0,iiii);
ui->comboBox->setModel(md);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM