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] Connect combobox using signals
Forum Updated to NodeBB v4.3 + New Features

[Solved] Connect combobox using signals

Scheduled Pinned Locked Moved General and Desktop
qcomboboxsignal
22 Posts 3 Posters 13.8k 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.
  • RatzzR Ratzz

    @p3c0
    But i have prblem with this

          combobox = new QComboBox();
          connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));
    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #5

    @Ratzz
    Would good if can share what the problem is. Otherwise nobody can give sound advice.

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #6

      @p3c0
      yes ,Declared m_combobox as a slot

      --Alles ist gut.

      p3c0P 1 Reply Last reply
      0
      • RatzzR Ratzz

        @p3c0
        yes ,Declared m_combobox as a slot

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #7

        @Ratzz Ok. Need more info. Do you get any specific error ?

        157

        1 Reply Last reply
        0
        • RatzzR Offline
          RatzzR Offline
          Ratzz
          wrote on last edited by p3c0
          #8

          mainwindow.h

          #include <QMainWindow>
          #include <QComboBox>
          #include <QWidget>
          #include <QPushButton>
          #include <QLabel>
          namespace Ui {
          class MainWindow;
          }
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
              
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
          
          
          private slots:
              void m_combobox();
              void m_pushbutton();
          
          private:
              Ui::MainWindow *ui;
              QComboBox *combobox;
              QPushButton *pushbutton;
              QLabel *label;
          };
          #endif // MAINWINDOW_H
          

          main.cpp

          #include <QtGui/QApplication>
          #include "mainwindow.h"
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
                  MainWindow w;
                  w.show();
                return a.exec();
          }
          

          mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
                ui->setupUi(this);
                combobox = new QComboBox();
                label = new QLabel("-");
                combobox->addItem("AAA");
                combobox->addItem("BBB");
                combobox->addItem("CCC");
                connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));
                pushbutton= new QPushButton("My Button", this);
                connect(pushbutton,SIGNAL (clicked()),this,SLOT(m_pushbutton()));
          
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          
          void MainWindow::m_combobox()
          {
              label->setText("1");
          }
          
          void MainWindow::m_pushbutton()
          {
          }
          

          I get only pushbutton at the output window.

          --Alles ist gut.

          p3c0P 1 Reply Last reply
          0
          • RatzzR Ratzz

            mainwindow.h

            #include <QMainWindow>
            #include <QComboBox>
            #include <QWidget>
            #include <QPushButton>
            #include <QLabel>
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
                
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            
            private slots:
                void m_combobox();
                void m_pushbutton();
            
            private:
                Ui::MainWindow *ui;
                QComboBox *combobox;
                QPushButton *pushbutton;
                QLabel *label;
            };
            #endif // MAINWINDOW_H
            

            main.cpp

            #include <QtGui/QApplication>
            #include "mainwindow.h"
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                    MainWindow w;
                    w.show();
                  return a.exec();
            }
            

            mainwindow.cpp

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                  ui->setupUi(this);
                  combobox = new QComboBox();
                  label = new QLabel("-");
                  combobox->addItem("AAA");
                  combobox->addItem("BBB");
                  combobox->addItem("CCC");
                  connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));
                  pushbutton= new QPushButton("My Button", this);
                  connect(pushbutton,SIGNAL (clicked()),this,SLOT(m_pushbutton()));
            
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            
            void MainWindow::m_combobox()
            {
                label->setText("1");
            }
            
            void MainWindow::m_pushbutton()
            {
            }
            

            I get only pushbutton at the output window.

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #9

            @Ratzz Ok few problems here:

            • You have not passed a parent to QComboBox and QLabel so you will need to call show() explicitly. Pass a parent as this for them like you did for QPushButton.
            • You have just created those widgets without assigning them a position and hence they will overlap eachother. Either use setGeometry() or move() to assign them a position or use Layouts. But using Layouts is a preferred way to do so. You can use QHBoxLayout or QVBoxLayout so that it takes care of positions.
            • The SLOTS function definition should match with that of the signal. You have missed a parameter for QComboBox. So it will not be able to find the slot and hence it won't work.

            157

            1 Reply Last reply
            1
            • RatzzR Offline
              RatzzR Offline
              Ratzz
              wrote on last edited by Ratzz
              #10

              I am a newbie to QT can you tell how to pass a parent to QComboBox / QLabel?
              I used below code Which opens the combobox and label in new window.But i want it in same mainwindow.

              combobox->show();
               label->show();

              --Alles ist gut.

              p3c0P 1 Reply Last reply
              0
              • RatzzR Ratzz

                I am a newbie to QT can you tell how to pass a parent to QComboBox / QLabel?
                I used below code Which opens the combobox and label in new window.But i want it in same mainwindow.

                combobox->show();
                 label->show();
                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by p3c0
                #11

                @Ratzz As said earlier just pass this. Thus it makes MainWindow parent of combobox and it will not open in new window. You have already done the same for pushbutton.
                Edit: Pass it in it's constructor.

                157

                1 Reply Last reply
                0
                • RatzzR Offline
                  RatzzR Offline
                  Ratzz
                  wrote on last edited by Ratzz
                  #12

                  @p3c0
                  thank you.
                  I used this
                  combobox = new QComboBox(this);
                  Is it the proper way to do it ?

                  --Alles ist gut.

                  p3c0P 1 Reply Last reply
                  0
                  • RatzzR Ratzz

                    @p3c0
                    thank you.
                    I used this
                    combobox = new QComboBox(this);
                    Is it the proper way to do it ?

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by p3c0
                    #13

                    @Ratzz Correct. Now the rest should work. Just don't forget to change the slot's definition.

                    157

                    1 Reply Last reply
                    0
                    • RatzzR Offline
                      RatzzR Offline
                      Ratzz
                      wrote on last edited by
                      #14

                      @p3c0 Why to change the slot definition?

                      --Alles ist gut.

                      p3c0P 1 Reply Last reply
                      0
                      • RatzzR Ratzz

                        @p3c0 Why to change the slot definition?

                        p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by
                        #15

                        @Ratzz Because in you have connected the signal activated to slot m_combobox which takes one argument.
                        connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));
                        But there is no such slot defined with single argument and thus the connection will fail as it will not be able to find that slot. You have a slot m_combobox() which takes no arguments.

                        157

                        1 Reply Last reply
                        0
                        • RatzzR Offline
                          RatzzR Offline
                          Ratzz
                          wrote on last edited by
                          #16

                          @p3c0
                          i used setGeometry() to assign position

                          •  pushbutton->setGeometry(QRect(QPoint(100, 200),QSize(90, 40)));
                            

                          How to do use move();?

                          --Alles ist gut.

                          p3c0P 1 Reply Last reply
                          0
                          • RatzzR Ratzz

                            @p3c0
                            i used setGeometry() to assign position

                            •  pushbutton->setGeometry(QRect(QPoint(100, 200),QSize(90, 40)));
                              

                            How to do use move();?

                            p3c0P Offline
                            p3c0P Offline
                            p3c0
                            Moderators
                            wrote on last edited by
                            #17

                            @Ratzz pushbutton->move(10,100). See http://doc.qt.io/qt-5/qwidget.html#pos-prop
                            But as said earlier its best to use Layouts. See using Layouts for more details.

                            157

                            1 Reply Last reply
                            1
                            • RatzzR Offline
                              RatzzR Offline
                              Ratzz
                              wrote on last edited by
                              #18

                              @p3c0 Can you give me rough sketch for my slot?

                              --Alles ist gut.

                              p3c0P 1 Reply Last reply
                              0
                              • RatzzR Ratzz

                                @p3c0 Can you give me rough sketch for my slot?

                                p3c0P Offline
                                p3c0P Offline
                                p3c0
                                Moderators
                                wrote on last edited by
                                #19

                                @Ratzz
                                connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int))); -> This is correct
                                mainwindow.h

                                private slots:
                                    void m_combobox(int index);
                                

                                mainwindow.cpp

                                void MainWindow::m_combobox(int index)
                                {
                                    label->setText("1");
                                }
                                

                                157

                                1 Reply Last reply
                                1
                                • RatzzR Offline
                                  RatzzR Offline
                                  Ratzz
                                  wrote on last edited by
                                  #20

                                  @p3c0
                                  Thank you.
                                  I will try using Layouts.

                                  --Alles ist gut.

                                  p3c0P 1 Reply Last reply
                                  0
                                  • RatzzR Ratzz

                                    @p3c0
                                    Thank you.
                                    I will try using Layouts.

                                    p3c0P Offline
                                    p3c0P Offline
                                    p3c0
                                    Moderators
                                    wrote on last edited by
                                    #21

                                    @Ratzz You're Welcome :)
                                    Since you are new here few more forum related reminders:

                                    • Always surround the code blocks with ``` (3 backticks) while posting so that it gets nicely formatted and thus more readable for others.
                                    • Mark the post as solved by editing the post title and prepending [Solved]

                                    Happy Qt Coding ...

                                    157

                                    1 Reply Last reply
                                    1
                                    • RatzzR Offline
                                      RatzzR Offline
                                      Ratzz
                                      wrote on last edited by
                                      #22

                                      @p3c0
                                      Sure :)

                                      --Alles ist gut.

                                      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