QCombobox内容居中(包含展示内容,下拉列表)

本文介绍了如何使QCombobox的内容及下拉列表居中显示,提供了两种方法:一是通过重写data函数,将Qt::TextAlignmentRole设置为Qt::AlignCenter;二是设置itemDelegate并自定义paint函数来居中绘制文本信息。
摘要由CSDN通过智能技术生成

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-&
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值