Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QComboBox in the QTableView
Forum Updated to NodeBB v4.3 + New Features

QComboBox in the QTableView

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 6.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pmjz
    wrote on last edited by
    #1

    Hi Guys,

    I want to directly display the QComboBox in the QTableView, not need to double click, Could anyone give me some ideas? thanks in advance.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Cayan
      wrote on last edited by
      #2

      Use QTableWidget and setCellWidget.
      A sample is shown below:
      @QComboBox *item = new QComboBox();
      ...
      table_widget->setCellWidget(row, column, item);@

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pmjz
        wrote on last edited by
        #3

        good idea,but I use QTableView not QTableWidget, I create my own delegate and model, but by this way,the QComboBox can not be displayed and shown directly. welcome any other suggestions

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cayan
          wrote on last edited by
          #4

          QTableView is inherited by QTableWidget, so there should not be any incompatibilities.
          AFAIK you won't do what you are willing to do without hard codding a custom QTableView.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pmjz
            wrote on last edited by
            #5

            excellent, If I have to use QTableView to display QComboBox directly? what should I do?

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              Have a look at "persistenteditor":http://developer.qt.nokia.com/doc/qt-4.7/qabstractitemview.html.

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pmjz
                wrote on last edited by
                #7

                @void ImportWellsDialog::setImportTableModel(QObject *obj)
                {
                QAbstractItemModel *model = qobject_cast<QAbstractItemModel *>(obj);
                delete this->m_importTableModel;
                m_importTableModel = model;
                if(model)
                {
                for( int i = 0; i < model->rowCount(); ++i )
                {
                // ui->importTableView->setIndexWidget(model->index(i, 3, QModelIndex()), new QComboBox);
                // ui->importTableView->openPersistentEditor(model->index(i, 3, QModelIndex()) );
                if(i == 1)
                {
                ui->importTableView->openPersistentEditor(model->index(i, 5, QModelIndex()));
                // ui->importTableView->setIndexWidget(model->index(i, 3, QModelIndex()), new QComboBox);
                }
                }

                }
                ui->importTableView->setModel(model);
                new MyHeader(ui->importTableView->horizontalHeader(),ui->importTableView);
                

                }@

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pmjz
                  wrote on last edited by
                  #8

                  @ImportWellsDialog::ImportWellsDialog(QWidget *parent) :
                  DialogBase(parent, false),
                  m_importTableModel(0),
                  ui(new Ui::ImportWellsDialog)
                  {
                  ui->setupUi(this);
                  setObjectName("ImportWellsDialog");
                  ui->importTableView->setShowGrid(false);
                  ui->importTableView->verticalHeader()->hide();
                  ui->importTableView->setItemDelegate(new CustomDelegate(ui->importTableView));
                  ui->importTableView->setItemDelegateForColumn(3,new WellsComboBoxDelegate(ui->importTableView));
                  ui->importTableView->setItemDelegateForColumn(5,new WellsComboBoxDelegate(ui->importTableView));
                  }
                  @

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pmjz
                    wrote on last edited by
                    #9

                    @QWidget *WellsComboBoxDelegate::createEditor(QWidget *parent,
                    const QStyleOptionViewItem &option,
                    const QModelIndex &index) const
                    {
                    Q_UNUSED(option);

                    //    if(index.column() == 2)
                    

                    // {
                    // for(int i = 0; i < list.size(); i++)
                    // {
                    // editor->addItem(list.at(i)->data(0).toString());//abbreviation's role is 0
                    // }
                    // Guard::checkIfTrue(QObject::connect(editor, SIGNAL(activated(int )),
                    // this, SLOT(comboBoxdataChanged(int))),HERE);
                    // }
                    if(((index.column() == 3)&&(index.row() != 0)) ||
                    ((index.column() == 5)&&(index.row() == 1)))
                    {
                    QComboBox *editor = new QComboBox(parent);
                    editor->setEditable(false);
                    editor->setAutoFillBackground(true);
                    return editor;

                    }
                    return 0;
                    

                    }@

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      What do you expect us to do with your code? Does it work now or not?

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved