Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QComboBox in a QTableView

QComboBox in a QTableView

Scheduled Pinned Locked Moved Mobile and Embedded
11 Posts 5 Posters 14.7k Views
  • 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.
  • T Offline
    T Offline
    theomarzz.nl
    wrote on last edited by
    #1

    Hello All,

    I'm making an app for Maemo & Symbian.
    In the app you can do stuff (not important).
    At the end you get an overview in a QTableView.
    I want the user to be able to edit the data of the overview.
    In one column the user can select stuff from a QComboBox.
    In the delegate I want to remove the QComboBox from the QTableView when the user moves to another field (in the QTableView or else where) and loses focus.

    The question is how do I do this, removing? The QComboBox doesn't seem to emit a signal when it loses focus...according to the documentation.

    Regards,

    Theo Kromhout van der Meer

    1 Reply Last reply
    0
    • B Offline
      B Offline
      blex
      wrote on last edited by
      #2

      Maybe this will help you: http://doc.trolltech.com/4.7.old/model-view-programming.html

      Quote:

      The standard QItemDelegate class informs the view when it has finished editing by emitting the closeEditor() signal. The view ensures that the editor widget is closed and destroyed. In this example, we only provide simple editing facilities, so we need never emit this signal.


      Oleksiy Balabay

      1 Reply Last reply
      0
      • I Offline
        I Offline
        infoctopus
        wrote on last edited by
        #3

        Subclass QItemDelegate, reimplement QWidget * QItemDelegate::createEditor()

        Also you might want to have a look at QItemEditorFactory class

        Qt rulez

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

          Better go with "QStyledItemDelegate":http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html as a base class to inherit from, instead of QItemDelegate. The former is the default anyways, so no surprises.

          You must reimplement the following virtual methods:

          @
          virtual QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
          virtual void setEditorData ( QWidget *editor, const QModelIndex &index ) const;
          virtual void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
          @

          In all three functions, first check if you are handling an index that needs a combo box. If no, then call the base class' functions (functions of QStyledItemDelegate with the same name) and return their value, if any.

          Otherwise:

          In createEditor() create and populate the combo box.

          In setEditorData() you select the right choice in the combo box according to the already available data of the index (if any). Call index.data(role) to get the data.

          In setModelData() get the selected value of your combo box and call model->setData(index, value, role) to save it in the model.

          The editor pointer is your combo box. You can cast it to QComboBox using qobject_cast. Check the casted pointer!

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

          1 Reply Last reply
          0
          • T Offline
            T Offline
            theomarzz.nl
            wrote on last edited by
            #5

            I've got 'C++ GUI Programming with Qt 4 2nd edition' here.
            Apparently I was steered the wrong direction by the book.
            In the last piece of chapter 10 'Implementing Custom Delegates'
            in the createEditor function:
            @
            connect(timeEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
            @
            the private method:
            @
            void TrackDelegate::commitAndCloseEditor()
            {
            QTimeEdit* editor = qobject_cast<QTimeEdit*>(sender());
            emit commitData(editor);
            emit closeEditor(editor);
            }
            @
            I thought that I had to do this for all the editors that I create.
            But if the book is wrong, that can happen.

            [edit: code highlighted / Denis Kormalev]

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on last edited by
              #6

              Theo Kromhout van der Meer, please use @ tags around your code. It will increase readability a lot.

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

                Could you please add @ tags around the connect statement, the arugments are missing.

                I don't know the book nor how they implement it and which base class they inherit from. I only use QStyledItemDelegate as a base class for my own delegates. Reimplementing the said methods do the trick for me. I do not reimplement more methods. One should consider updateEditorGeometry(), too to get the combo boxes' sizes done right.

                I'll create a wiki article with some working code.

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

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DenisKormalev
                  wrote on last edited by
                  #8

                  Volker, done.

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

                    Thank's, Denis.

                    I would bet that the connect statement generates a warning about a non-existing signal on runtime. editingFinished() is a signal of QLineEdit (but we're talking about combo boxes) or of QAbstractSpinBox (e.g. used by a QTimeEdit, also not a combo box).

                    If you want to go this way, you would have to connect signal QComboBox::activated() to your slot; this is emitted when the user has choosen an item.

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

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      theomarzz.nl
                      wrote on last edited by
                      #10

                      No, no.
                      Above, I just rewrote the example whats in the book. The example there used a QTimeEdit field as an editor. That is why I asked my question. I want to use a QComboBox but I saw that doesn't work that way, because it doesn't have a similar editingFinished signal.

                      I just wanted to know how to solve this.

                      But I am all for it to leave the above code out and give it a try.
                      My N900 is running Qt 4.6. I have to do some coding and then I will run it in the simulator and then on the N900 and I will just test it.

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

                        I just added the wiki article "Combo Boxes in Item Views ":http://developer.qt.nokia.com/wiki/Combo_Boxes_in_Item_Views. You can use this sample code as a starting point.

                        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