1.本文达到效果如下:

2.代码如下
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
resize(600, 400);
QComboBox* box = new QComboBox (this);
m_lineeidt = new QLineEdit (box);
m_lineeidt->setObjectName("lineedit");
m_lineeidt->setAutoFillBackground(true);
m_lineeidt->setStyleSheet("QLineEdit#lineedit,QLineEdit#lineedit:disabled{ border: none; font: 13px \"Microsoft YaHei\"; color: black; margin: 0px 20px 0px 1px;background: white;}");
connect(box, SIGNAL(currentIndexChanged(const QString&)), m_lineeidt, SLOT(setText(const QString&)));
QHBoxLayout* layoutBox = new QHBoxLayout; layoutBox->setSpacing(0);layoutBox->setMargin(0);
box->setLayout(layoutBox);
layoutBox->addWidget(m_lineeidt);
// box->setLi

本文介绍了如何使QCombobox的内容及下拉列表居中显示,提供了两种方法:一是通过重写data函数,将Qt::TextAlignmentRole设置为Qt::AlignCenter;二是设置itemDelegate并自定义paint函数来居中绘制文本信息。
最低0.47元/天 解锁文章
1430

被折叠的 条评论
为什么被折叠?



