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. where is my button? Problem with QComboBox
Forum Updated to NodeBB v4.3 + New Features

where is my button? Problem with QComboBox

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 521 Views 2 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    Hi,

    I have very strange problem with QComboBox. I have a subclass of QComboBox, where I add QStandardItems to QStandardItemModel. Next I set this model. And the last thing is add QPushButton to first cell. When I click on this button I would like to see text "click" on qDebug. When I have only a few items everything is ok.

    The problem is when I have many items in this comboBox ( many = I see slider when click on comboBox ).

    This situation when there is error:

    1. I click on comboBox's line Edit to show popup.

    2. I move slider ( or using mouse wheel ) to don't see the first cell with QPushButton

    3. click somewhere ( outside comboBox, click on comboBox's line Edit ) to hide popup

    4. I click on comboBox's line Edit to show popup - there is no QPushButton

    And picture:

    combobox.png

    1. here I click the first time in comboBox's line Edit
    2. here I move slider to don't see the first cell
      between 2) and 3) I click outside comboBox to hide popup
    3. here I click the second time - there is no QPushButton ( I check clicking on first row - I don't see "click" )

    My code:
    In mainWindow I only add #include "mycombobox.h" and in costructor I only add line

    myCombo = new myComboBox(this);
    

    And myComboBox class:

    #ifndef MYCOMBOBOX_H
    #define MYCOMBOBOX_H
    #include <QComboBox>
    #include <QStandardItem>
    #include <QStandardItemModel>
    class myComboBox:public QComboBox
    {
        Q_OBJECT
    public:
        myComboBox(QWidget *parent = nullptr);
        QStandardItemModel *itemModel;
    private slots:
        void buttonClick();
    };
    
    #endif // MYCOMBOBOX_H
    
    #include "mycombobox.h"
    #include <QAbstractItemView>
    #include <QPushButton>
    #include <QDebug>
    myComboBox::myComboBox(QWidget *parent): QComboBox(parent)
    {
        QStringList textList;
        itemModel = new QStandardItemModel;
        textList<<""<<"a"<<"b"<<"c"<<"d"<<"e"<<"f"<<"g"<<"h"<<"i"<<"j"<<"k"<<"l"<<"m"<<"n"<<"o"<<"p"<<"r"<<"s"<<"t";
    
        for(auto text: textList)
        {
            QStandardItem* item = new QStandardItem(text);
            itemModel->setItem(itemModel->rowCount(), 0, item);
        }
        setModel(itemModel);
    
        // add button to first cell
        QPushButton * button = new QPushButton;
        QModelIndex index = model()->index(0,0);
        connect(button, SIGNAL(clicked()), this, SLOT(buttonClick()));
        view()->setIndexWidget(index, button);
    }
    
    void myComboBox::buttonClick()
    {
        qDebug()<<"click";
    }
    
    Pl45m4P 1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      For some reason, it gets a negative y pos.
      QRect(0,-130 81x13)

      So it seems the scrolling moves it out of the viewports rect.

      If i do
      c->button->move(0,0);
      (c is your combo)

      its back again in right place so it seems like a bug with QComboBox and setIndexWidget.

      alt text

      1 Reply Last reply
      3
      • T TomNow99

        Hi,

        I have very strange problem with QComboBox. I have a subclass of QComboBox, where I add QStandardItems to QStandardItemModel. Next I set this model. And the last thing is add QPushButton to first cell. When I click on this button I would like to see text "click" on qDebug. When I have only a few items everything is ok.

        The problem is when I have many items in this comboBox ( many = I see slider when click on comboBox ).

        This situation when there is error:

        1. I click on comboBox's line Edit to show popup.

        2. I move slider ( or using mouse wheel ) to don't see the first cell with QPushButton

        3. click somewhere ( outside comboBox, click on comboBox's line Edit ) to hide popup

        4. I click on comboBox's line Edit to show popup - there is no QPushButton

        And picture:

        combobox.png

        1. here I click the first time in comboBox's line Edit
        2. here I move slider to don't see the first cell
          between 2) and 3) I click outside comboBox to hide popup
        3. here I click the second time - there is no QPushButton ( I check clicking on first row - I don't see "click" )

        My code:
        In mainWindow I only add #include "mycombobox.h" and in costructor I only add line

        myCombo = new myComboBox(this);
        

        And myComboBox class:

        #ifndef MYCOMBOBOX_H
        #define MYCOMBOBOX_H
        #include <QComboBox>
        #include <QStandardItem>
        #include <QStandardItemModel>
        class myComboBox:public QComboBox
        {
            Q_OBJECT
        public:
            myComboBox(QWidget *parent = nullptr);
            QStandardItemModel *itemModel;
        private slots:
            void buttonClick();
        };
        
        #endif // MYCOMBOBOX_H
        
        #include "mycombobox.h"
        #include <QAbstractItemView>
        #include <QPushButton>
        #include <QDebug>
        myComboBox::myComboBox(QWidget *parent): QComboBox(parent)
        {
            QStringList textList;
            itemModel = new QStandardItemModel;
            textList<<""<<"a"<<"b"<<"c"<<"d"<<"e"<<"f"<<"g"<<"h"<<"i"<<"j"<<"k"<<"l"<<"m"<<"n"<<"o"<<"p"<<"r"<<"s"<<"t";
        
            for(auto text: textList)
            {
                QStandardItem* item = new QStandardItem(text);
                itemModel->setItem(itemModel->rowCount(), 0, item);
            }
            setModel(itemModel);
        
            // add button to first cell
            QPushButton * button = new QPushButton;
            QModelIndex index = model()->index(0,0);
            connect(button, SIGNAL(clicked()), this, SLOT(buttonClick()));
            view()->setIndexWidget(index, button);
        }
        
        void myComboBox::buttonClick()
        {
            qDebug()<<"click";
        }
        
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @TomNow99

        What, if you use QPushButton as delegate for the first item?

        @mrjj btw: I think setIndexWidget is buggy in general or can lead to some weird behavior. It's @VRonin 's best friend, according to the signature :D

        Edit:

        setIndexWidget documentation also says:

        This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QStyledItemDelegate instead.

        (https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget)

        It says visible area... So I don't know, what happens, if you scroll and move the widget out of the visible area.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        mrjjM 1 Reply Last reply
        3
        • Pl45m4P Pl45m4

          @TomNow99

          What, if you use QPushButton as delegate for the first item?

          @mrjj btw: I think setIndexWidget is buggy in general or can lead to some weird behavior. It's @VRonin 's best friend, according to the signature :D

          Edit:

          setIndexWidget documentation also says:

          This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QStyledItemDelegate instead.

          (https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget)

          It says visible area... So I don't know, what happens, if you scroll and move the widget out of the visible area.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Pl45m4
          Hehe well its fair to say VRonin is not thrilled about setIndexWidget in general but its mostly due to being very heavy and
          not the right way.

          But this issues does not happen with QListWidget (even if scrolling ) so I assume its due to how ComboBox works with its viewport.

          1 Reply Last reply
          2

          • Login

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