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. Is it possible to replace visible QComboBox values with an custom text?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to replace visible QComboBox values with an custom text?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 466 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hey everyone,

    I have a QComboBox that is populated with data via a QDataWidgetMapper with a model. This model contains an ID and an image file from the database.

    Let's assume that the data set stored in the QComboBox has three values. For illustration I take the ID field and not the Blob field. The QComboBox will now display the following:

    7036
    7036
    7036

    This shows me now that I can select 3 images from a QComboBox. Of course it would be nicer if the 7036 becomes an image 1, 2, 3, so for example:

    7036 -> Image 1
    7036 -> Image 2
    7036 -> Image 3

    Is such a thing possible and if so how? Unfortunately I could not find anything on the Internet.Below is another screenshot. I would like to replace these numbers with Image1, Image 2, Image 3.

    66f18365-d1c2-4430-9e4c-7d588ef96052-grafik.png

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #5

      Thank you very much for your answers. After many attempts to get the whole thing to work, I have then decided for a somewhat easier way. I have simply created a new field with "filename" in my database table. I now display this in the QComboBox and use it to select the data.

      1 Reply Last reply
      0
      • SeDiS Offline
        SeDiS Offline
        SeDi
        wrote on last edited by SeDi
        #2

        Hi,
        I have not tried it, I am by no means an expert and haven't used QWidgets for years, but if you want to do complicated things I'd try to inherit from QComboBox and overwrite

        QString QComboBoxPrivate::itemText(const QModelIndex &index) const
        {
            return index.isValid() ? model->data(index, itemRole()).toString() : QString();
        }
        

        with something that gets the proper text displayed.

        But wouldn't it suffice to state the visible column?
        The docs seem to suggest that:

        modelColumn : int
        This property holds the column in the model that is visible.
        
        If set prior to populating the combo box, the pop-up view will not be affected and will show the first column (using this property's default value).
        
        By default, this property has a value of 0.
        
        Note: In an editable combobox, the visible column will also become the completion column.
        
        Access functions:
        
        int	modelColumn() const
        void	setModelColumn(int visibleColumn)
        
        

        But, as stated before, all of this is just a guess ymmv.

        1 Reply Last reply
        0
        • faduF Offline
          faduF Offline
          fadu
          wrote on last edited by
          #3

          @Gabber
          hi
          are you try somthing like this

           for(int i=-1;i<ui->comboBox->count();i++)
              {
                  ui->comboBox->setItemText(i,"Image " + QString::number(i));
              }
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            QComboBox uses Qt's item view system so you can use a custom QStyledItemDelegate to show what you want.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #5

              Thank you very much for your answers. After many attempts to get the whole thing to work, I have then decided for a somewhat easier way. I have simply created a new field with "filename" in my database table. I now display this in the QComboBox and use it to select the data.

              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