I'm trying to connect a button click to a function, and pass an int value, but keep getting an error: no matching function for call to Main::connect... I'm guessing that I'm not initializing something correct?
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setupUI();
}
MainWindow::~MainWindow() {
}
void MainWindow::setupUI() {
QFrame* frame = new QFrame(this);
_layout = new QVBoxLayout;
frame->setLayout(_layout);
parseXML();
QScrollArea* scrollArea = new QScrollArea;
scrollArea->setWidget(frame);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
}
void MainWindow::parseXML() {
this->parseItem(xml, count)
}
QMap<QString, QString> MainWindow::parseItem(QXmlStreamReader& xml, int count) {
QString valueName = "buttonid";
QSignalMapper *signalMapper = new QSignalMapper(this);
QPushButton* button = new QPushButton(valueName);
_layout->addWidget(button);
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(addMenu(int)));
signalMapper->setMapping(button, valueName);
connect(button, SIGNAL(clicked())), signalMapper, SIGNAL(map());
return something;
}
void MainWindow::addMenu(int count) {
_layoutToAdd = new QVBoxLayout;
QPushButton* button = new QPushButton("New Button");
_layoutToAdd->addWidget(button);
_layout->insertLayout(count, _layoutToAdd, 0);
}
mainwindow.h
ublic:
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void parseXML();
void addMenu(int count);
signals:
void clicked();
There's a syntax error in your code. Count your parenthesis :
connect(button, SIGNAL(clicked())), signalMapper, SIGNAL(map());
// ^ ^ ^^^^
// 1 2 3321 <-- All your parenthesis are closed at this point
// meaning you are calling connect() with only 2 parameters
You must white Q_OBJECT (for using signals and slots ) in descriction of your class
class MainWindow: public QObject
{
Q_OBJECT
Related
So lets say I a QDialog class like this
class DiagExample : public QDialog
{
Q_OBJECT
public:
DiagExample(QWidget *parent);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
};
And the implementation looks like this
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, [=](){myIntValue++;});
connect(MinusToValue, &QPushButton::released, [=](){myIntValue--;});
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(myIntValue, ???, [=](int newValue){
counter->setText(QString::number(newValue);});
}
I know that I could go straight from QPushButton::released -> setText on the QLabel, but my code will eventually have many inputs feeding into the counter, so from a point of readability and simplicity, I would rather have this sort of paradigm --- INPUT_WIDGET -> myIntValue -> setText(NEW VALUE OF myIntValue).
Standard way of doing such things in Qt:
class DiagExample : public QDialog
{
Q_OBJECT
Q_PROPERTY(intValue READ intValue NOTIFY onIntValueChange)
public:
DiagExample(QWidget *parent);
int intValue() const;
signals:
void onIntValueChange(int);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
};
Now in cpp:
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, [=](){
myIntValue++;
emit onIntValueChange(myIntValue);
});
connect(MinusToValue, &QPushButton::released, [=](){
myIntValue--;
emit onIntValueChange(myIntValue);
});
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(this, &DiagExample::onIntValueChange, [=](int newValue){
counter->setText(QString::number(newValue);});
}
int DiagExample::intValue() const {
return myIntValue;
}
Note only QObject can emit signals!
You will have to define it yourself, something like that could work:
class DiagExample : public QDialog
{
Q_OBJECT
public:
DiagExample(QWidget *parent);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
private slots:
void onPlusOne();
void onMinusOne();
signals:
void intValueChanged(const QString& newVal);
};
and then in you cpp file:
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, &DiagExample::onPlusOne);
connect(MinusToValue, &QPushButton::released, &DiagExample::onMinusOne);
connect(this, &DiagExample::intValueChanged, counter, &QLabel::setText);
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(myIntValue, ???, [=](int newValue){
counter->setText(QString::number(newValue);});
}
void DiagExample::onPlusOne()
{
myIntValue++;
emit intValueChanged(QString::number(myIntValue));
}
void DiagExample::onMinusOne()
{
myIntValue--;
emit intValueChanged(QString::number(myIntValue));
}
My problem was right-clicking the Qpushbutton it will show the items in it. how can we write this code using Qt C++? before I kept a combo box for the menu but now I need to keep the pushbutton Rightclick event to open the items?
""""""".h"""""""""
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
Bc *open;
Rt *open1;
Mt * open2;
//QRightclickbutton *open3;
//QRightClickButton(QWidget *parent = 0);
~MainWindow();
//protected:
//void mousePressEvent(QMouseEvent *e);
signals:
void rightClicked();
protected:
// void mousePressEvent(QMouseEvent * event);
void pushbutton(QWidget *parent);
void mousePressEvent(QMouseEvent *e);
private slots:
//bool eventFilter(QObject *obj, QEvent *e);
void on_pb1_clicked();
void on_pb2_clicked();
void on_pb3_clicked();
void on_pb4_clicked();
// void on_pushButton_clicked();
// void on_pushButton_clicked(bool checked);
void on_pb_rightclicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H`
""Mainwindow.cpp""
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Unical Project Title");
QMenu *menu = new QMenu(this);
menu->addAction("BC");
menu->addAction("RT");
menu->addAction("MT");
ui-> pb->setMenu(menu);
// connect(ui->pushButton, SIGNAL(Rightclicked), this, SLOT(play()));
// ui->pushButton->viewport()-> installEventFilter(this);
open = new Bc;
open1= new Rt;
open2= new Mt;
QObject *w = new QObject;
// QString *select = new QString;
ui->device0->addItem("Set as ");
ui->device0->addItem("BC");
ui->device0->addItem("RT");
ui->device0->addItem("MT");
//QObject::connect(ui->device0, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
// open->show();
ui->device1->addItem("Set as");
ui->device1->addItem("BC");
ui->device1->addItem("RT");
ui->device1->addItem("MT");
ui->device2->addItem("Set as");
ui->device2->addItem("BC");
ui->device2->addItem("RT");
ui->device2->addItem("MT");
ui->device3->addItem("Set as");
ui->device3->addItem("BC");
ui->device3->addItem("RT");
ui->device3->addItem("MT");
// int row= 7;
// int column= 2;
for (int row =0; row<=ui->tablewidget->rowCount(); row++)
{
QCheckBox * cb = new QCheckBox(this);
cb->setCheckState(Qt::Checked);
QWidget *w =new QWidget ();
QHBoxLayout *hLayout =new QHBoxLayout();
hLayout->addWidget(cb);
hLayout->setMargin(0);
hLayout->setAlignment(cb,Qt::AlignCenter);
w->setLayout(hLayout);
ui->tablewidget->setCellWidget(row,2,w);
}
}
MainWindow::~MainWindow()
{
delete UI;
}
void MainWindow::on_pb1_clicked()
{
ui->device0->currentText();
if(ui->device0->currentText() == "BC")
{
QObject::connect(ui->device0, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open->show();
}
if (ui->device0->currentText() == "RT")
{
QObject::connect(ui->device0, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open1->show();
}
if (ui->device0->currentText() == "MT")
{
QObject::connect(ui->device0, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open2->show();
}
}
void MainWindow::on_pb2_clicked()
{
ui->device1->currentText();
if(ui->device1->currentText() == "BC")
{
QObject::connect(ui->device1, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open->show();
}
if (ui->device1->currentText() == "RT")
{
QObject::connect(ui->device1, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open1->show();
}
if (ui->device1->currentText() == "MT")
{
QObject::connect(ui->device1, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open2->show();
}
}
void MainWindow::on_pb3_clicked()
{
ui->device2->currentText();
if(ui->device2->currentText() == "BC")
{
QObject::connect(ui->device2, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open->show();
}
if (ui->device2->currentText() == "RT")
{
QObject::connect(ui->device2, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open1->show();
}
if (ui->device2->currentText() == "MT")
{
QObject::connect(ui->device2, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open2->show();
}
}
void MainWindow::on_pb4_clicked()
{
ui->device3->currentText();
if(ui->device3->currentText() == "BC")
{
QObject::connect(ui->device3, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open->show();
}
if (ui->device3->currentText() == "RT")
{
QObject::connect(ui->device0, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open1->show();
}
if (ui->device3->currentText() == "MT")
{
QObject::connect(ui->device3, SIGNAL(currentTextChanged(QString)),this,SLOT(setText(QString)));
open2->show();
}
}
//void MainWindow::on_pushButton_clicked(bool checked)
//{
// QMouseEvent *button= new QMouseEvent(this);
// ui->pushButton->button;
// connect(button, SIGNAL(rightClicked()), this, SLOT(()));
// qDebug()<<"right clicked";
//}
void pushbutton ::on_pb_rightclicked()
{
connect(ui->pushButton, SIGNAL(rightClicked),this, SLOT (menuBar(show())));
}
//void pushbutton::on_pb_clicked()
//{
//}
what I want is while Rightclick the Qpushbutton it shows the items in it and please help me to do this. thank you
You could use a custom context menu for the QPushButton.
First, you will need to assign the button context menu policy. So when you initialize your MainWindow, you should also:
ui->pushButton->setContextMenuPolicy(Qt::CustomContextMenu);
Then create a slot for when the custom context menu is requested (when the button is right-clicked):
//Your .h
private slots:
void on_pushButton_customContextMenuRequested(const QPoint &pos);
// Your .cpp
void MainWindow::on_pushButton_customContextMenuRequested(const QPoint &pos)
{
QMenu contextMenu(tr("Context menu"), this);
QAction action1("BC", this);
connect(&action1, &QAction::triggered,
[]{qDebug() << "User selected BC";});
contextMenu.addAction(&action1);
QAction action2("RT", this);
connect(&action2, &QAction::triggered,
[]{qDebug() << "User selected RT";});
contextMenu.addAction(&action2);
QAction action3("MT", this);
connect(&action3, &QAction::triggered,
[]{qDebug() << "User selected MT";});
contextMenu.addAction(&action3);
contextMenu.exec(mapToGlobal(pos));
}
I'm not sure what you want to have done when the user selects an item from the context menu, so the above uses a lambda function when each action is triggered. Alternately you could use a traditional signal/slot connection.
In the future, please try to include only the code for a MRE
I am newbie in GUI design. Here, in sending and receiving messages (float, integer values) using DDS (OpenSplice) I am trying to add a pushButton to my already existing labels(displaying some float values as shown below), so that after a clicking on the pushButton, I should be able to see data in my label.
I tried adding a push button with the help of Qt Network sender Example. Now I get the error undefined reference to 'MainWindow::on_pushButton_clicked() in the file moc_mainwindow.cpp while building the project.
fastsender.cpp
FastSender::FastSender(QLabel *x, QObject *parent) : QObject(parent)
{
wSend = x;
dataTimer = new QTimer(this);
emergency = new QPushButton(tr("Emergency"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(emergency,QDialogButtonBox::ActionRole);
connect(emergency, SIGNAL(clicked()), this, SLOT(startsending()));
connect(dataTimer, SIGNAL(timeout()), this, SLOT(walk()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(buttonBox);
}
void FastSender::startsending()
{
emergency->setEnabled(false);
dataTimer->start(100); // Interval 0 means to refresh as fast as possible
}
int FastSender::walk()
{
msg->value= i+0.1;
snprintf (buf, MAX_MSG_LEN, "Message no. %d", i);
cout << "Writing message: \"" << msg->value << "\"" << endl;
status = talker->write(*msg, userHandle);
QString s= QString::number(msg->value, 'f',8);
wSend->setText(s);
}
fastsender.h
class FastSender : public QObject
{
Q_OBJECT
public:
explicit FastSender(QObject *parent = 0);
FastSender(QLabel *x, QObject *parent = 0);
~FastSender();
signals:
private:
QTimer* dataTimer;
QLabel *wSend;
DDS::DomainParticipantFactory_var dpf;
DDS::DomainParticipant_var parentDP;
DDS::Topic_var signalTopic;
DDS::DataReader_var parentReader;
DDS::DataWriter_var parentWriter;
fw::signalSeq_var msgSeq;
char * signalTypeName;
fw::signalDataWriter_var talker;
fw::signal *msg;
DDS::InstanceHandle_t userHandle;
DDS::Publisher_var fwPublisher;
int alpa;
QPushButton *emergency;
QDialogButtonBox *buttonBox;
public slots:
int walk();
int hello();
void startsending();
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fwdds = new FastWanDDS(ui->label);//receiving values are displayed in label
fwdds1 = new FastSender(ui->label_2);//Sending values are displayed in label_2
}
mainwindow.h
Ui::MainWindow *ui;
QTimer* timer;
int counter;
FastWanDDS *fwdds;
FastSender *fwdds1;
Any Help Appreciated.
Note: Only snippets of the code presented
I got it to work this way. Does this work for you?
mainwindow.cpp
#include "mainwindow.h"
#include "fastsender.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QLabel* label = new QLabel("unchanged");
FastSender* fs = new FastSender(label);
setCentralWidget(fs);
}
MainWindow::~MainWindow()
{
}
fastsender.h
#ifndef FASTSENDER_H
#define FASTSENDER_H
#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>
class FastSender : public QWidget
{
Q_OBJECT
public:
FastSender(QLabel* label, QWidget* parent = 0)
: QWidget(parent)
, _label(label)
, _timer(new QTimer)
, _emergency(new QPushButton(tr("Emergency")))
, _bbox(new QDialogButtonBox)
{
_bbox->addButton(_emergency, QDialogButtonBox::ActionRole);
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(_label);
layout->addWidget(_bbox);
setLayout(layout);
connect(_emergency, SIGNAL(clicked(bool)), this, SLOT(startsending()));
connect(_timer, SIGNAL(timeout()), this, SLOT(walk()));
}
public slots:
void startsending()
{
_emergency->setEnabled(false);
_timer->start(100);
}
int walk()
{
_label->setText("changed");
_emergency->setEnabled(true);
}
private:
QLabel* _label;
QTimer* _timer;
QPushButton* _emergency;
QDialogButtonBox* _bbox;
};
#endif // FASTSENDER_H
I have a project that I want to add button dynamically wherever I click in my form.
This is my header:
namespace Ui {
class frmBedBook;
}
class frmBedBook : public QWidget
{
Q_OBJECT
public:
explicit frmBedBook(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *event);
~frmBedBook();
private:
Ui::frmBedBook *ui;
QSignalMapper *signalMapper;
QList<QPushButton*> buttonList;
QGridLayout *lyWidget;
QWidget *m_widget;
public slots:
void clicked(int buttonId);
};
And this is my implementation:
frmBedBook::frmBedBook(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmBedBook)
{
ui->setupUi(this);
signalMapper = new QSignalMapper();
QPushButton *p;
lyWidget = new QGridLayout();
m_widget = new QWidget();
m_widget->setGeometry(0,0,930,472);
lyWidget->setContentsMargins(0,0,0,0);
lyWidget->addWidget(m_widget);
setLayout(lyWidget);
p = new QPushButton(m_widget);
p->setText("00");
p->setGeometry(0, 0, 50, 50);
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,0);
p = new QPushButton(m_widget);
p->setText("01");
p->setGeometry(50, 0, 50, 50);
p->setObjectName("01");
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,1);
connect(signalMapper, SIGNAL(mapped(int)),this, SLOT(clicked(int)));
}
void frmBedBook::mousePressEvent(QMouseEvent *event)
{
QPushButton *p;
p = new QPushButton(m_widget);
p->setText("02");
p->setGeometry(QCursor::pos().x(), QCursor::pos().y(), 50, 50);
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,2);
}
The problem is the button is created, but not visible. I know it because I have traced through m_widget's children and it's found. I also already resetting layout in MousePressEvent function, but nothing happened. Could anyone please help me about this problem?
You need to call show() on your buttons if they are added after the form has been constructed. Also, QCursor::pos() will probably not deliver the position you want to have.
You can use the x()/y() functions of the QMouseEvent instead:
void frmBedBook::mousePressEvent(QMouseEvent *event)
{
QPushButton *p;
p = new QPushButton(m_widget);
p->setText("02");
p->setGeometry(event->x(), event->y(), 50, 50);
p->show();
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,2);
}
Note that you need to #include <QtGui/QMouseEvent> unless you already have that.
You never show the buttons, so they stay hidden. Use QWidget::show(). This applies to widgets created in mousePressEvent, after parent is already shown. The button created in constructor should get automatically shown when its parent is shown.
I am considering transitioning between menu screens in a game by using QStateMachine. However, I'm unsure how to kick off some code (e.g. show() a QWidget) upon a transition between states occurring. I can do it quite easily with plain old signals (see commented out code), but I figure that I could probably do some fancy animation upon switching screens using transitions.
Here's my code:
Edit: updated as per Koying's suggestion.
ApplicationWindow.h:
#include <QtGui>
#include <QStateMachine>
#include "MainMenu.h"
#include "LoadGameMenu.h"
class ApplicationWindow : public QMainWindow
{
Q_OBJECT
public:
ApplicationWindow();
private slots:
void mainMenuButtonClicked();
void loadGameMenuButtonClicked();
private:
MainMenu* mainMenu;
LoadGameMenu* loadGameMenu;
QStateMachine stateMachine;
QStackedWidget* stack;
};
ApplicationWindow.cpp:
ApplicationWindow::ApplicationWindow()
{
resize(800, 600);
stack = new QStackedWidget(this);
mainMenu = new MainMenu();
setCentralWidget(mainMenu);
loadGameMenu = new LoadGameMenu();
QState* mainMenuState = new QState();
QState* loadGameMenuState = new QState();
QAbstractTransition* loadTransition = mainMenuState->addTransition(
mainMenu, SIGNAL(loadGameClicked()), loadGameMenuState);
connect(loadTransition, SIGNAL(triggered()), this, SLOT(loadGameMenuButtonClicked()));
QAbstractTransition* mainMenuTransition = loadGameMenuState->addTransition(
loadGameMenu, SIGNAL(backToMainMenuClicked()), mainMenuState);
connect(mainMenuTransition, SIGNAL(triggered()), this, SLOT(mainMenuButtonClicked()));
stateMachine.addState(mainMenuState);
stateMachine.addState(loadGameMenuState);
stateMachine.setInitialState(mainMenuState);
stateMachine.start();
}
void ApplicationWindow::mainMenuButtonClicked()
{
setCentralWidget(mainMenu);
}
void ApplicationWindow::loadGameMenuButtonClicked()
{
setCentralWidget(loadGameMenu);
}
LoadGameMenu.h:
#include <QtGui>
class LoadGameMenu : public QWidget
{
Q_OBJECT
public:
LoadGameMenu();
signals:
void backToMainMenuClicked();
private:
QPushButton* loadGameButton;
QPushButton* backToMainMenuButton;
};
LoadGameMenu.cpp:
#include "LoadGameMenu.h"
LoadGameMenu::LoadGameMenu()
{
loadGameButton = new QPushButton(tr("Load"));
backToMainMenuButton = new QPushButton(tr("Main Menu"));
QObject::connect(backToMainMenuButton, SIGNAL(clicked()),
this, SIGNAL(backToMainMenuClicked()));
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(loadGameButton);
layout->addWidget(backToMainMenuButton);
layout->setContentsMargins(300, 400, 300, 200);
setLayout(layout);
}
MainMenu.h:
#include <QtGui>
class MainMenu : public QWidget
{
Q_OBJECT
public:
MainMenu();
signals:
void newGameClicked();
void loadGameClicked();
private slots:
void exit();
private:
QPushButton* newGameButton;
QPushButton* loadGameButton;
QPushButton* exitGameButton;
QMenu* fileMenu;
};
MainMenu.cpp:
#include "MainMenu.h"
MainMenu::MainMenu()
{
newGameButton = new QPushButton(tr("New Game"), this);
loadGameButton = new QPushButton(tr("Load Game"));
exitGameButton = new QPushButton(tr("Exit"));
QObject::connect(newGameButton, SIGNAL(clicked()), this, SIGNAL(newGameClicked()));
QObject::connect(loadGameButton, SIGNAL(clicked()), this, SIGNAL(loadGameClicked()));
QObject::connect(exitGameButton, SIGNAL(clicked()), qApp, SLOT(quit()));
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(newGameButton);
layout->addWidget(loadGameButton);
layout->addWidget(exitGameButton);
layout->setContentsMargins(300, 200, 300, 200);
setLayout(layout);
}
void MainMenu::exit()
{
if( QMessageBox::question(
this,
tr("Exit?"),
tr("Do you really want to exit the game?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No
) == QMessageBox::Yes
)
{
qApp->quit();
}
}
main.cpp:
#include <QtGui>
#include "ApplicationWindow.h"
int main(int argv, char **args)
{
QApplication app(argv, args);
ApplicationWindow window;
window.show();
return app.exec();
}
So, how do I trigger some behaviour or action when a transition occurs?
Cheers.
To actually do something on a state transition, you have to connect to the triggered() signal of the transition, e.g.
QAbstractTransition* trMainLoad = mainMenuState->addTransition(mainMenu, SIGNAL(loadGameClicked()), loadGameMenuState);
connect(trMainLoad , SIGNAL(triggered()), SLOT(...));