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 doesn't works change event

QComboBox doesn't works change event

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 2.2k 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.
  • M Offline
    M Offline
    MyNameIsQt
    wrote on last edited by MyNameIsQt
    #1

    Change item event on combo box doesn't work.
    If you look up a YouTube video, it shows selecting a combobox item and pressing a button.
    Doesn't signal work when changing combobox item?
    If so, why am I not working?
    As a test, I made a push button and added a click signal event. But this also doesn't work.

    void ResizeDlg::on_comboWidthSize_currentIndexChanged(int index)
    {
     qDebug() << "test";
      ....
    }
    
    void ResizeDlg::on_pushButton_clicked()
    {
        qDebug() << "test";
    }
    

    캡처21.JPG
    캡처22.JPG

    Christian EhrlicherC 1 Reply Last reply
    0
    • M MyNameIsQt

      @Christian-Ehrlicher
      Did you mean this?

      ResizeDlg::ResizeDlg(QWidget *parent)
          : QDialog(parent)
          , ui(new Ui::ResizeDlg)
      {
          ui->setupUi(this);
      
          QStringList widthSizeOpt;
          widthSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters";
          ui->comboWidthSize->addItems(widthSizeOpt);
          ui->comboWidthSize->setCurrentIndex(2);
          QStringList heightSizeOpt;
          heightSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters";
          ui->comboHeightSize->addItems(heightSizeOpt);
          ui->comboHeightSize->setCurrentIndex(2);
      
          QObject::connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(on_comboWidthSize_currentIndexChanged(int)));
      }
      

      But this doesn't work!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #4

      @MyNameIsQt
      It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:

      connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged);
      connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked);
      
      connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } );
      connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
      
      M 2 Replies Last reply
      1
      • M MyNameIsQt

        Change item event on combo box doesn't work.
        If you look up a YouTube video, it shows selecting a combobox item and pressing a button.
        Doesn't signal work when changing combobox item?
        If so, why am I not working?
        As a test, I made a push button and added a click signal event. But this also doesn't work.

        void ResizeDlg::on_comboWidthSize_currentIndexChanged(int index)
        {
         qDebug() << "test";
          ....
        }
        
        void ResizeDlg::on_pushButton_clicked()
        {
            qDebug() << "test";
        }
        

        캡처21.JPG
        캡처22.JPG

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Please do not use the auto-connect stuff but connect the signals and slots by yourself. I would guess the name of your combobox is not comboWidthSize

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        M 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Please do not use the auto-connect stuff but connect the signals and slots by yourself. I would guess the name of your combobox is not comboWidthSize

          M Offline
          M Offline
          MyNameIsQt
          wrote on last edited by
          #3

          @Christian-Ehrlicher
          Did you mean this?

          ResizeDlg::ResizeDlg(QWidget *parent)
              : QDialog(parent)
              , ui(new Ui::ResizeDlg)
          {
              ui->setupUi(this);
          
              QStringList widthSizeOpt;
              widthSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters";
              ui->comboWidthSize->addItems(widthSizeOpt);
              ui->comboWidthSize->setCurrentIndex(2);
              QStringList heightSizeOpt;
              heightSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters";
              ui->comboHeightSize->addItems(heightSizeOpt);
              ui->comboHeightSize->setCurrentIndex(2);
          
              QObject::connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(on_comboWidthSize_currentIndexChanged(int)));
          }
          

          But this doesn't work!

          JonBJ 1 Reply Last reply
          0
          • M MyNameIsQt

            @Christian-Ehrlicher
            Did you mean this?

            ResizeDlg::ResizeDlg(QWidget *parent)
                : QDialog(parent)
                , ui(new Ui::ResizeDlg)
            {
                ui->setupUi(this);
            
                QStringList widthSizeOpt;
                widthSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters";
                ui->comboWidthSize->addItems(widthSizeOpt);
                ui->comboWidthSize->setCurrentIndex(2);
                QStringList heightSizeOpt;
                heightSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters";
                ui->comboHeightSize->addItems(heightSizeOpt);
                ui->comboHeightSize->setCurrentIndex(2);
            
                QObject::connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(on_comboWidthSize_currentIndexChanged(int)));
            }
            

            But this doesn't work!

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @MyNameIsQt
            It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:

            connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged);
            connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked);
            
            connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } );
            connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
            
            M 2 Replies Last reply
            1
            • JonBJ JonB

              @MyNameIsQt
              It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:

              connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged);
              connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked);
              
              connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } );
              connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
              
              M Offline
              M Offline
              MyNameIsQt
              wrote on last edited by
              #5

              @JonB

              .h file

              #include <QComboBox>
              class ResizeDlg : public QDialog
              {
              private slots:
                  void convertPixel();
                  void convertSize();
              ...
              }
              

              .cpp file

              ResizeDlg::ResizeDlg(QWidget *parent)
                  : QDialog(parent)
                  , ui(new Ui::ResizeDlg)
              {
                  ui->setupUi(this);
              
                  connect(ui->comboWidth, SIGNAL(currentIndexChanged(int)), this, SLOT(convertPixel()));
                  connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(convertSize()));
              }
              
              void ResizeDlg::convertPixel()
              {
                  QMessageBox::information(nullptr, "test", "text.");
                  qDebug() << "Hello";
              }
              
              void ResizeDlg::convertSize()
              {
                     QMessageBox::information(nullptr, "test", "text.");    
                      qDebug() << "Hello";    
              }
              

              Application Output

              qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertPixel() in ..\WpubViewer\resizedlg.cpp:33
              qt.core.qobject.connect: QObject::connect:  (sender name:   'comboWidth')
              qt.core.qobject.connect: QObject::connect:  (receiver name: 'ResizeDlg')
              qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertSize() in ..\WpubViewer\resizedlg.cpp:34
              qt.core.qobject.connect: QObject::connect:  (sender name:   'comboWidthSize')
              qt.core.qobject.connect: QObject::connect:  (receiver name: 'ResizeDlg')
              

              Why does this message occur?

              Christian EhrlicherC 1 Reply Last reply
              0
              • M MyNameIsQt has marked this topic as solved on
              • JonBJ JonB

                @MyNameIsQt
                It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:

                connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged);
                connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked);
                
                connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } );
                connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
                
                M Offline
                M Offline
                MyNameIsQt
                wrote on last edited by MyNameIsQt
                #6

                @JonB Thanks. It was solved thanks to you.
                :)

                  connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertPixel);
                    connect(ui->comboWidth, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertSize);
                
                JonBJ 1 Reply Last reply
                0
                • M MyNameIsQt

                  @JonB Thanks. It was solved thanks to you.
                  :)

                    connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertPixel);
                      connect(ui->comboWidth, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertSize);
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @MyNameIsQt
                  I am glad you have changed over to the Qt New Signal Slot Syntax, which was brought in a decade ago. I recommend you now use this and no longer SIGNAL/SLOT() macros going forward.

                  1 Reply Last reply
                  0
                  • M MyNameIsQt

                    @JonB

                    .h file

                    #include <QComboBox>
                    class ResizeDlg : public QDialog
                    {
                    private slots:
                        void convertPixel();
                        void convertSize();
                    ...
                    }
                    

                    .cpp file

                    ResizeDlg::ResizeDlg(QWidget *parent)
                        : QDialog(parent)
                        , ui(new Ui::ResizeDlg)
                    {
                        ui->setupUi(this);
                    
                        connect(ui->comboWidth, SIGNAL(currentIndexChanged(int)), this, SLOT(convertPixel()));
                        connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(convertSize()));
                    }
                    
                    void ResizeDlg::convertPixel()
                    {
                        QMessageBox::information(nullptr, "test", "text.");
                        qDebug() << "Hello";
                    }
                    
                    void ResizeDlg::convertSize()
                    {
                           QMessageBox::information(nullptr, "test", "text.");    
                            qDebug() << "Hello";    
                    }
                    

                    Application Output

                    qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertPixel() in ..\WpubViewer\resizedlg.cpp:33
                    qt.core.qobject.connect: QObject::connect:  (sender name:   'comboWidth')
                    qt.core.qobject.connect: QObject::connect:  (receiver name: 'ResizeDlg')
                    qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertSize() in ..\WpubViewer\resizedlg.cpp:34
                    qt.core.qobject.connect: QObject::connect:  (sender name:   'comboWidthSize')
                    qt.core.qobject.connect: QObject::connect:  (receiver name: 'ResizeDlg')
                    

                    Why does this message occur?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @MyNameIsQt said in QComboBox doesn't works change event:

                    Why does this message occur?

                    Because you did not add the Q_OBJECT macro

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    JonBJ 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @MyNameIsQt said in QComboBox doesn't works change event:

                      Why does this message occur?

                      Because you did not add the Q_OBJECT macro

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #9

                      @Christian-Ehrlicher
                      Ah, I didn't know that must apply with old-style connect. I nearly mentioned this to user, but it seems it all works with new-style syntax. My understanding is you need Q_OBJECT if you emit signals, but you can get away without if you only connect/use slots, right?

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Christian-Ehrlicher
                        Ah, I didn't know that must apply with old-style connect. I nearly mentioned this to user, but it seems it all works with new-style syntax. My understanding is you need Q_OBJECT if you emit signals, but you can get away without if you only connect/use slots, right?

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @JonB said in QComboBox doesn't works change event:

                        My understanding is you need Q_OBJECT if you emit signals, but you can get away without if you only connect/use slots, right?

                        No, you need it when you want qobject_cast<> or old style connect or any other QMetaObject feature.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        JonBJ 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @JonB said in QComboBox doesn't works change event:

                          My understanding is you need Q_OBJECT if you emit signals, but you can get away without if you only connect/use slots, right?

                          No, you need it when you want qobject_cast<> or old style connect or any other QMetaObject feature.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #11

                          @Christian-Ehrlicher
                          I cannot test, but I thought if you have

                          signals:
                              void someSignal();
                          

                          you also need Q_OBJECT else you get some moc error or something?

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Christian-Ehrlicher
                            I cannot test, but I thought if you have

                            signals:
                                void someSignal();
                            

                            you also need Q_OBJECT else you get some moc error or something?

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @JonB Maybe a linker error (vtable) - don't know.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            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