我有一个QComboBox,上面有很多东西:
combo->addItem("def");
combo->addItem("abc");现在,我想将QWidget添加到我的一个项目中,例如:
QPushButton *button = new QPushButton;
button->setStyleSheet("QPushButton {background:red}");
button->setFixedSize(10,10);
QModelIndex index = combo->model()->index(0,0);
combo->view()->setIndexWidget(index, button);我将按钮的大小设置为10x10 (当然我的QComboBox更大)。我想把这个按钮移到其他地方(图)。

发布于 2020-10-07 17:50:51
您可以尝试使用布局
QPushButton *button = new QPushButton;
button->setStyleSheet("QPushButton {background:red}");
button->setFixedSize(10,10);
auto widget = new QWidget{this};
auto layout = new QHBoxLayout{widget};
layout->setContentsMargins(0, 0, 0, 0);
layout->addStretch();
layout->addWidget(button);
QModelIndex index = combo->model()->index(0,0);
combo->view()->setIndexWidget(index, widget);https://stackoverflow.com/questions/64224709
复制相似问题