当前位置: 首页 > news >正文

上海普陀门户网站湖南网站建设公司 在线磐石网络

上海普陀门户网站,湖南网站建设公司 在线磐石网络,外贸流程知乎,包工头接活网站app目录 1. 需求提出 2. 解决方法 1. 需求提出 QComboBox下拉项中的文本默认是居左的#xff0c;如下#xff1a; 有时需要将下拉项中的文本居中、居右。如何实现#xff1f; 2. 解决方法 首先想到的是通过样式表来解决#xff0c;但找遍Qt Assist和网络#xff0c;都没这…目录 1. 需求提出 2. 解决方法 1. 需求提出 QComboBox下拉项中的文本默认是居左的如下 有时需要将下拉项中的文本居中、居右。如何实现 2. 解决方法 首先想到的是通过样式表来解决但找遍Qt Assist和网络都没这样的样式表那就代码实现吧本文用到了Qt的model/view framework框架,如果对Qt的“模型/视图/委托”框架不懂本博文很难读懂。如果不懂这方面的知识请在Qt Assistant 中输入Model/View Programming 学习了解。读者本机Qt安装目录下的Examples\Qt-XX.XX.XX\widgets\itemviews目录下有很多model/view framework的例子可以进行自学了解其中XX.XX.XX为Qt的版本号如5.14.1。代码如下 comboBoxModel.h #ifndef COMBOBOXMODEL_H #define COMBOBOXMODEL_H #include QAbstractItemModel class CComboBoxModel :public QAbstractItemModel { public:CComboBoxModel(QObject* parent nullptr);public:// 设置项的文本内容void addComboBoxContent(const QString qsItemContent);// 设置项的文本内容void addComboBoxContent(const QStringList lstItemContent);// 设置项文本对齐方式void setItemTextAlignMent(Qt::Alignment eAlignment); public: // virtualvirtual QModelIndex index(int row, int column, const QModelIndex parent QModelIndex()) const;virtual QVariant data(const QModelIndex index, int role Qt::DisplayRole) const; private:virtual int columnCount(const QModelIndex parent QModelIndex()) const;virtual QModelIndex parent(const QModelIndex index) const;virtual int rowCount(const QModelIndex parent QModelIndex()) const;private:QStringList m_lstComboBoxContent;Qt::Alignment m_eAlignment{ Qt::AlignLeft}; // 项文本默认左对齐};#endif COMBOBOXMODEL_HcomboBoxModel.cpp #include comboBoxModel.h CComboBoxModel::CComboBoxModel(QObject* parent /* nullptr*/):QAbstractItemModel(parent) {}// 对于QComboBox只有1列 int CComboBoxModel::columnCount(const QModelIndex parent /* QModelIndex()*/) const {return 1; }// 根据项的行列索引取得项在模型中的模型索引。注意对于QComboBox只有1列 QModelIndex CComboBoxModel::index(int row, int column, const QModelIndex parent/* QModelIndex()*/) const {return createIndex(row, column); }// 根据项的角色取得该角色对应的属性 QVariant CComboBoxModel::data(const QModelIndex index, int role /* Qt::DisplayRole*/) const {if (!index.isValid()){return QVariant();}if (Qt::TextAlignmentRole role) // 项文本对齐属性{return QVariant(m_eAlignment);// Qt::AlignCenter;}else if(Qt::DisplayRole role) // 项文本属性{auto rowIndex index.row();return m_lstComboBoxContent[rowIndex];}return QVariant(); }QModelIndex CComboBoxModel::parent(const QModelIndex index) const {return QModelIndex(); }// 项个数 int CComboBoxModel::rowCount(const QModelIndex parent/* QModelIndex()*/) const {return m_lstComboBoxContent.size(); }// 设置项的文本内容 void CComboBoxModel::addComboBoxContent(const QString qsItemContent) {m_lstComboBoxContent.append(qsItemContent); }// 设置项的文本内容 void CComboBoxModel::addComboBoxContent(const QStringList lstItemContent) {m_lstComboBoxContent lstItemContent; }void CComboBoxModel::setItemTextAlignMent(Qt::Alignment eAlignment) {m_eAlignment eAlignment; } QtWidgetsApplication1.cpp #include QtWidgetsApplication1.h #include comboBoxModel.h #includeQLineEdit QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent): QWidget(parent) {ui.setupUi(this);auto pModel new CComboBoxModel(this);QStringList lst;lst QStringLiteral(开机) QStringLiteral(关机) QStringLiteral(复位);pModel-addComboBoxContent(lst);auto eAlignment Qt::AlignCenter;pModel-setItemTextAlignMent(eAlignment);ui.comboBox-setModel(pModel);auto pLineEdit new QLineEdit;pLineEdit-setAlignment(eAlignment);pLineEdit-setReadOnly(true);ui.comboBox-setLineEdit(pLineEdit);m_qsLineEdit lst[0];pLineEdit-setText(m_qsLineEdit);connect(ui.comboBox, QOverloadint::of(QComboBox::activated),[](int index) { pLineEdit-setText(m_qsLineEdit); });connect(ui.comboBox, QOverloadint::of(QComboBox::currentIndexChanged),[](int index){ auto modelIndex pModel-index(index, 0); // 获取选择的项的模型索引auto text pModel-data(modelIndex).toString(); // 根据模型索引从模型中取出项文本pLineEdit-setText(text); // 将项的文本设置到编辑框m_qsLineEdit text;}); }其中m_qsLineEdit为在QtWidgetsApplication1.h定义的、用来保存上次选择的文本。注意一定要响应编辑框的activated信号否则在下拉框中选择新的项时编辑框将会清空上次的文本这样很不友好如下 如下为项文本居中的最终效果 可以通过CComboBoxModel类的setItemTextAlignMent函数来设置对齐方式。其它的代码注释很明白就不再解释了。
http://www.pierceye.com/news/529985/

相关文章:

  • 丹东市做网站广东做网站的公司
  • 网站收录大全销售推广
  • 网站发展历程东莞企业网站建设制作
  • 厦门市建设局查询保障摇号网站首页做房产网站长
  • 公司网站建设的普遍性长沙建站网
  • 东莞英文建站公司api模式网站开发
  • 参与网站网站建设可判几年大同住房和城乡建设网站
  • 住房和城乡建设部网站投诉电话哈尔滨网页设计网站模板
  • 西安网站制作哪家公司好阳江房产网最新楼盘江楼盘
  • 科技馆网站建设网络营销实务教案
  • 网站上线倒计时html5模板文创产品设计流程
  • 建设项目竣工环保验收公示网站网站建设具体实施方案
  • 蜜淘app在那个网站做的濮阳吧
  • 建网站市场哪家网站开发好
  • 找生产厂家的网站宁波seo运营推广平台排名
  • 淘宝的网站建设的目标昌大建设集团是哪里的
  • 毕业设计h5网站制作较好的网站设计师培训学校
  • 做淘宝券推广的网站有哪些石碣镇做网站
  • 贵阳网站制作免费桂林市工程建设项目招标网站
  • 南昌专业网站排名推广做网站编辑我能力得到提升
  • 网站建设行业凡科建站登录入口官方正版
  • 一般网站开发的硬件要求视觉冲击力的网站设计
  • 昆明做网站建设的公司哪家好广西建设培训网
  • 国字类型网站有哪些内容百度seo分析工具
  • 免费浪漫网页制作网站html的常用标签
  • 上行10m做网站服务仿简书wordpress博客主题
  • 网站建设中网页代码做网站的是不是程序员
  • 做网站需要的技术做网站优化好的网络公司
  • 为什么要更新网站登录四川省住房和城乡建设厅官网
  • 注册网站不用手机短信验证的网站营销型网站的建设起步