【问题标题】:How to display short value in QComboBox and a long value in it's dropdown?如何在 QComboBox 中显示短值并在其下拉列表中显示长值?
【发布时间】:2023-03-15 14:20:01
【问题描述】:

我正在尝试让 QComboBox 显示一个较短的值(例如,“1”、“2”或“3”),但让组合框的下拉列表显示较长的值,例如“数字一”, “二号”或“三号”。

这就是我所拥有的......

QString max_dropdown_len, styleSheet;

max_dropdown_len = QString::number(300);
styleSheet = "QComboBox QAbstractItemView { min-width: %1; }";

cb = new QComboBox();

model = qobject_cast<QStandardItemModel*>(cb->model());
model->setColumnCount(2);
model->appendRow(QList<QStandardItem*>() <<
    (new QStandardItem("Number One")) << (new QStandardItem("1")));
model->appendRow(QList<QStandardItem*>() <<
    (new QStandardItem("Number Two")) << (new QStandardItem("2")));
model->appendRow(QList<QStandardItem*>() <<
    (new QStandardItem("Number Three")) << (new QStandardItem("3")));

view = qobject_cast<QListView*>(cb->view());
view->setModelColumn(0);

cb->setModelColumn(1);
cb->setStyleSheet(styleSheet.arg(max_dropdown_len));

aLayout = new QFormLayout;
aLayout->addRow(new QLabel(tr("Test: ")), cb);

aGroup = new QGroupBox(tr("Testing"));
aGroup->setLayout(aLayout);

return aGroup;

问题是,组合框只显示一个“1”,当我单击箭头下拉列表时,选项是“1”、“2”或“3”,而不是较长的字符串值.

我这样做是为了显示一个短值吗?想法?

【问题讨论】:

    标签: c++ qt qt5 qcombobox


    【解决方案1】:

    一种可能的解决方案是将长文本添加为​​UserData,并为选择长文本的QComboBox 建立一个委托。

    #include <QApplication>
    #include <QComboBox>
    #include <QStandardItemModel>
    #include <QStyledItemDelegate>
    
    class ComboBoxDelegate: public QStyledItemDelegate{
    public:
        using QStyledItemDelegate::QStyledItemDelegate;
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override{
            QStyleOptionViewItem opt(option);
            initStyleOption(&opt, index);
            opt.text = index.data(Qt::UserRole).toString();
            const QWidget *widget = opt.widget;
            QStyle *style = widget ? widget->style() : QApplication::style();
            style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QComboBox combo;
        combo.addItem("1", "Number One");
        combo.addItem("2", "Number Two");
        combo.addItem("3", "Number Three");
        combo.setItemDelegate(new ComboBoxDelegate(&combo));
        combo.show();
        return a.exec();
    }
    

    【讨论】:

      【解决方案2】:

      使用项目模型,您必须创建一个插槽:

      cb->setModelColumn(0);
      

      当显示项目列表时,会这样做:

      cb->setModelColumn(1);
      

      当项目被隐藏时。 这会很复杂,因为 QComboBox 在释放时不会发出信号,因此您必须创建自己的类。

      相反,使用 QListView 更简单:

      Widget::Widget(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Widget)
      {
          ui->setupUi(this);
      
          int comboBox_lenght = ui->comboBox->count();
      
          listView = new QListView(ui->comboBox);
      
          ui->comboBox->setView(listView);
      
          for (int i=0; i<comboBox_lenght; ++i){ //fill the combobox with hidden items
              ui->comboBox->addItem(QString::number(i + 1));
              listView->setRowHidden(comboBox_lenght + i, true);
          }
      
          on_comboBox_currentIndexChanged(ui->comboBox->currentIndex());
      
          show();
      }
      

      具有以下插槽:

      void Widget::on_comboBox_currentIndexChanged(int index)
      {
          ui->comboBox->setCurrentText(QString::number(index + 1));
      
          return;
      }
      

      结果:https://drive.google.com/file/d/16ZSvaG8req5ztCyqrIup23NQqixYcEpP/view?usp=sharing

      【讨论】:

      • 他没有,但您的插槽假定组合是可编辑的。
      • 好吧,因为我使用的是 QListView,所以没有必要让它可编辑。试试看或观看截屏视频...
      • 好的,现在我明白了你的意思,但你只提供了很少的代码并且没有解释。你提出的想法很好,但需要解释:)
      • 好的我明白了,下次我尽量解释
      【解决方案3】:

      大部分有问题的代码都是正确的,但使用QListView 可能并不方便。这可以通过QTableView 来完成,它提供了许多丰富的选项,可以根据需要进行微调,这是使用 QTableView 的简单实现。

      QString max_dropdown_len, styleSheet;
      max_dropdown_len = QString::number(300);
      styleSheet = "QComboBox QAbstractItemView { min-width: %1; }";
      QStandardItemModel* model = new QStandardItemModel(this);
      model->setColumnCount(2);
      model->appendRow(QList<QStandardItem*>() <<
          (new QStandardItem("1")) << (new QStandardItem("Number One")));
      model->appendRow(QList<QStandardItem*>() <<
          (new QStandardItem("2")) << (new QStandardItem("Number Two")));
      model->appendRow(QList<QStandardItem*>() <<
          (new QStandardItem("3")) << (new QStandardItem("Number Three")));
      QTableView* view = new QTableView(this);
      view->setModel(model);
      ui->comboBox->setModel(model);
      ui->comboBox->setModelColumn(0);
      ui->comboBox->setStyleSheet(styleSheet.arg(max_dropdown_len));
      ui->comboBox->setView(view);
      view->horizontalHeader()->setHidden(true);
      view->verticalHeader()->setHidden(true);
      view->setGridStyle(Qt::NoPen); // Table without Grid
      view->setColumnWidth(0,0); // Option: set this width to also show the digits during combo popup, set to 0 to hide them!
      

      【讨论】:

      • 抱歉这么晚才回来。这是使用 Qt 4.x 吗?我用的是5.9。只是将 QListView 换成 QTableView 给了我与我的 OP 相同的问题。我希望我能让这个工作,因为它对我来说似乎更简单,并且适合我已经实现的。
      • 继续上面的评论,在大多数情况下,它工作正常,这意味着当显示“1”时,小部件的宽度最小,而当出现下降时,宽度更宽包含长值。但我无法在下拉列表中选择一个值。
      • 我不知道您的要求是针对 Qt 4.x,但我认为它可以,因为它使用基本模型。
      • 对 4.x 没有要求,我只是想知道我似乎没有 setHidden() 调用。无论如何,这样做对我来说似乎很简单,但由于某种原因我还没有让它工作。
      • 我认为是因为您没有直接单击文本..问题是我只使用了原始示例代码..您可能需要将选择单元格的宽度调整为可用间距。跨度>
      猜你喜欢
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多