Reputation: 1046
I just started learning QT and what i wanted to accomplish is to get popup message on buttons click. Here is how my file looks like:
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow *mainWindow = new MainWindow();
QLabel *text = new QLabel("Some text");
QPushButton *btn = new QPushButton("Click");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(btn);
layout->addWidget(text);
QObject::connect(btn, SIGNAL(clicked()), &app, SLOT(popup()));
mainWindow->setLayout(layout);
mainWindow->show();
return app.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::popUp()
{
QMessageBox::information(this, "New Box", "Message");
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void popUp();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Can you please explain what i've done wrong or maybe there is something missing in my code.
Upvotes: 2
Views: 2171
Reputation: 243887
I recommend that the graphical part implement it inside the class MainWindow
since the member ui
that is private handles the design.
#include <QMessageBox>
#include <QLabel>
#include <QLayout>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel *text = new QLabel("Some text");
QPushButton *btn = new QPushButton("Click");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(btn);
layout->addWidget(text);
ui->centralWidget->setLayout(layout);
connect(btn, &QPushButton::clicked, this, &MainWindow::popUp);
}
Do not make any changes to main.cpp
, the code should look like this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Upvotes: 2
Reputation: 69
Ok, you have to work on the file mainwindow.cpp
. You have already create the slot, so connect it adding this code in MainWindow's constructor (if you don't know witch is the mw constructor it is the function where is written ui->setupUi(this);
)
connect(ui->btn, SIGNAL(clicked()), SLOT(popUp()));
now in the slot MainWindow::popUp();
put this code:
QMessageBox msgBox;
msgBox.setText("text to write");
msgBox.exec();
remember to include QMessageBox
in mainwindow.cpp, you shouldn't write code on main.cpp, it must be this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Upvotes: 0
Reputation: 935
You're connecting to a private slot from outside mainwindow. The application output probably shows you a warning like this:
QObject::connect: No such slot QApplication::popup()
Usually what you'd want to do is put stuff used inside mainwindow, inside mainwindow class.
Eg: Create the layout inside the constructor.
You can use the parent argument when creating the objects to get them destroyed when you're done with mainwindow.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel *text = new QLabel("Some text", this);
QPushButton *btn = new QPushButton("Click", this);
QHBoxLayout *layout = new QHBoxLayout;
QObject::connect(btn, SIGNAL(clicked()), this, SLOT(popUp()));
ui->centralWidget->setLayout(layout);
this->show();
}
You have to use ui->centralWidget
, because that's how mainwindow works. The application output should have given you a warning for that as well.
Like this:
QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout
Upvotes: 0
Reputation: 2832
Wrong connection. You should connect the clicked()
signal to the mainWindow
's slot popUp()
, because it is a member of the MainWindow
class. And don't forget about case sensitivity of c++ (popUp, not popup):
QObject::connect(btn, SIGNAL(clicked()), mainWindow, SLOT(popUp()));
Upvotes: 0