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. [SOLVED] ComboBox of CheckBoxes
QtWS25 Last Chance

[SOLVED] ComboBox of CheckBoxes

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 17.8k 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.
  • S Offline
    S Offline
    soroush
    wrote on last edited by
    #1

    Hi

    I need a combo box of check boxes. It should be able to connect signals of its checkboxes to some slots. also show selected choices in label. for example if user selects ch1 and ch2 and ch3 the label should something like this: "Selection: ch1, ch2 ch3" and if no check box is checked, it should be "Please select...".

    I guess I should play around models / views to obtain such widget. In documentation mentioned that QCombBox uses models and views internally. And it's possible to set it's model.

    What should I do?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dialingo
      wrote on last edited by
      #2

      Yes, your idea works. Here is a very easy way to test it:

      Get the following example from Qt example folder:
      examples/tutorials/modelview/2_formatting

      Now replace the QTableView with a QComboBox. The checkboxes which were meant for the QTableView are now visible in the QComboBox;

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        You are on the right track. You can subclass QComboBox, and use your own model for the options list. For that model, you can set the Qt::UserCheckable flag, so a checkbox is displayed. Then, listen to changes in the model data, and update the text accordingly.

        However, that is where the problems start. I don't see a straightforward way to manipulate the text of a non-editable QComboBox widget. I would have expected a virtual method for that. You could probably hack around this by using an editable QComboBox and manipulating the QLineEdit (you have access to it) to be in fact non-editable and basically look like a label. Not the nicest solution, but it should work.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dialingo
          wrote on last edited by
          #4

          bq. I don’t see a straightforward way to manipulate the text of a non-editable QComboBox widget.

          Andre, why do you think it is not possible to implement the setData() method of the model to collect mouseclicks on checkboxes?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            soroush
            wrote on last edited by
            #5

            Ok, I sublass QAbstractListModel and populate a new model containing a list of QCheckBox* s. Now displays checkboxes correctly but I couldn't managed to do editing things:
            @
            MyModel::MyModel(QObject parent)
            :QAbstractListModel(parent)
            {
            for(int i=0; i<10; i++)
            {
            QCheckBox
            x = new QCheckBox;
            x->setText(QString("Check Box %1").arg(i));
            list.append(x);
            }
            }

            int MyModel::rowCount(const QModelIndex & /*parent */) const
            {
            return list.length();
            }

            int MyModel::columnCount(const QModelIndex & /*parent */) const
            {
            return 1;
            }

            QVariant MyModel::data(const QModelIndex &index;, int role) const
            {
            if(role == Qt::CheckStateRole)
            return list[index.row()]->checkState();
            if(role == Qt::DisplayRole)
            return list[index.row()]->text();
            if(role == Qt::EditRole)
            {
            list[index.row()]->setCheckState( list[index.row()]->checkState() == Qt::Checked ? Qt::Unchecked:Qt::Checked );
            }

            return QVariant();
            

            }

            bool MyModel::setData(const QModelIndex &index;, const QVariant &value;, int role)
            {
            // This doesn't work
            list[index.row()]
            ->setCheckState( list[index.row()]->checkState() == Qt::Checked ? Qt::Unchecked:Qt::Checked );
            qDebug()<<role; // Anything... So method is not called ?
            }
            @

            1 Reply Last reply
            0
            • S Offline
              S Offline
              soroush
              wrote on last edited by
              #6

              I set the editable flag and use the QLineEdit to show which choices are selected. Works fine

              Thanks

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="dialingo" date="1310568540"]bq. I don’t see a straightforward way to manipulate the text of a non-editable QComboBox widget.

                Andre, why do you think it is not possible to implement the setData() method of the model to collect mouseclicks on checkboxes?[/quote]
                I don't think that at all. Where do I say that? I was talking about the currentText property of QComboBox.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  ksrkuk
                  wrote on last edited by
                  #8

                  soroush,
                  I tried to use same code on Ubuntu 12.04. In addition to your code I added
                  Qt::ItemFlags MyModel::flags(const QModelIndex & /index/) const
                  {
                  return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable ;
                  }
                  Still Combo box not listing check box, only lists the label.
                  Your help is much appreciated.

                  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