[Solved] Connect combobox using signals



  • I want to add new combobox and connect with signals.
    For pushbutton i used Below code .
    How can i connect combobox?

    pushbutton= new QPushButton("My Button", this);
    connect(pushbutton,SIGNAL (clicked()),this,SLOT(m_pushbutton()));

  • Moderators

    Hi @Ratzz,
    In the same way connect one or more of these signals to slot(s).



  • @p3c0
    But i have prblem with this

          combobox = new QComboBox();
          connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));

  • Moderators

    @Ratzz What problem ? Any errors ? Declared m_combobox as a slot ?



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



  • @p3c0
    yes ,Declared m_combobox as a slot


  • Moderators

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



  • 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.


  • Moderators

    @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.


  • 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();

  • Moderators

    @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.



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


  • Moderators

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



  • @p3c0 Why to change the slot definition?


  • Moderators

    @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.



  • @p3c0
    i used setGeometry() to assign position

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

    How to do use move();?


  • Moderators

    @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.



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


  • Moderators

    @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");
    }
    


  • @p3c0
    Thank you.
    I will try using Layouts.


  • Moderators

    @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 ...



  • @p3c0
    Sure :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.