[Solved] Connect combobox using signals
-
wrote on 27 Jun 2015, 11:46 last edited by Ratzz
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()));
-
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()));
-
@p3c0
But i have prblem with thiscombobox = new QComboBox(); connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));
@Ratzz What problem ? Any errors ? Declared
m_combobox
as a slot ? -
@p3c0
But i have prblem with thiscombobox = new QComboBox(); connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));
-
@Ratzz Ok. Need more info. Do you get any specific error ?
-
wrote on 27 Jun 2015, 12:13 last edited by p3c0
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.
-
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.
@Ratzz Ok few problems here:
- You have not passed a parent to
QComboBox
andQLabel
so you will need to callshow()
explicitly. Pass a parent asthis
for them like you did forQPushButton
. - You have just created those widgets without assigning them a position and hence they will overlap eachother. Either use
setGeometry()
ormove()
to assign them a position or use Layouts. But using Layouts is a preferred way to do so. You can useQHBoxLayout
orQVBoxLayout
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.
- You have not passed a parent to
-
wrote on 27 Jun 2015, 12:38 last edited by 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();
-
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();
@Ratzz As said earlier just pass
this
. Thus it makesMainWindow
parent ofcombobox
and it will not open in new window. You have already done the same forpushbutton
.
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 ?@Ratzz Correct. Now the rest should work. Just don't forget to change the slot's definition.
-
@Ratzz Because in you have connected the signal
activated
to slotm_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 slotm_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();?
@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. -
-
@Ratzz
connect(combobox,SIGNAL (activated(int)),this,SLOT(m_combobox(int)));
-> This is correct
mainwindow.hprivate slots: void m_combobox(int index);
mainwindow.cpp
void MainWindow::m_combobox(int index) { label->setText("1"); }
1/22