> 文章列表 > QComboBox里的每个选项加入多个控件

QComboBox里的每个选项加入多个控件

QComboBox里的每个选项加入多个控件

自定义类头文件:

class ComboboxItem : public QWidget
{Q_OBJECTpublic:ComboboxItem(QWidget *parent);~ComboboxItem();private:QCheckBox *cBox;QPushButton *btn;QHBoxLayout *layout;
};

自定义类源文件

ComboboxItem::ComboboxItem(QWidget *parent): QWidget(parent)
{setContentsMargins(0, 0, 0, 0);cBox = new QCheckBox("123", this);btn = new QPushButton("ggg", this);btn->setStyleSheet("QPushButton{background-color:rgb(255,0,0);border: 1px solid #000000;border-radius: 11px;}");layout = new QHBoxLayout(this);layout->addWidget(cBox);layout->addWidget(btn);layout->setStretchFactor(cBox, 4);layout->setStretchFactor(btn, 1);layout->setSpacing(0);layout->setContentsMargins(0, 0, 0, 0);setLayout(layout);
}ComboboxItem::~ComboboxItem()
{
}

主程序调用:

box = new QComboBox(this);
box->setGeometry(100, 100, 188, 31);
box->setEditable(true);
box->lineEdit()->setReadOnly(true);listWidget = new QListWidget(this);
box->setModel(listWidget->model());
box->setView(listWidget);
// 在下拉框中添加5个选项
for (int i = 0; i < 5; ++i)
{ComboboxItem* comboItem = new ComboboxItem(this);QListWidgetItem* widgetItem = new QListWidgetItem(listWidget);widgetItem->setSizeHint(comboItem->sizeHint());listWidget->addItem(widgetItem);listWidget->setItemWidget(widgetItem, comboItem);
}

字体转换器