Qt: Return in main from button_click - c++

As you can find (if you look at few of mine previous questions) I am pretty new with Qt. I am making "read-from-excel-file-push-to-some-DB" module.
I want to get path wich push button returns in main.cpp
So, long story short here's my code:
fb_test.h
#ifndef FB_TEST_H
#define FB_TEST_H
#include <QtGui/QMainWindow>
#include "ui_fb_test.h"
class FB_test : public QMainWindow
{
Q_OBJECT
public:
FB_test(QWidget *parent = 0, Qt::WFlags flags = 0);
~FB_test();
private:
Ui::FB_testClass ui;
public slots:
QString on_pushButton_clicked();
};
#endif // FB_TEST_H
fb_test.cpp
#include <QtGui>
#include "fb_test.h"
FB_test::FB_test(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
}
FB_test::~FB_test()
{
}
QString FB_test::on_pushButton_clicked()
{
QString path;
path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null);
return path;
}
main.cpp
#include <QApplication>
#include <QtGui>
#include <QtSql>
#include <QAxObject>
#include <QAxWidget>
#include "fb_test.h"
bool initExcel(QAxObject* &excelApp);
void getTableHeaders(QAxObject* _worksheet, QAxObject* _excel);
bool readExcelFile(QAxObject* excel, QString& file_path, QString& selected_list);
void getTableHeaders(QAxObject* _worksheet, QAxObject* _excel);
//....
//here's methods above implementation
//....
void excelTest(){
QAxObject* excel;
QString path = QString("C:\\databases\\oilnstuff.xls");//gonna use GUI choose
QString list = QString("list1");
if(initExcel(excel)){
if (readExcelFile(excel, path, list)){
//
}else{
//error output
}
}
excel->dynamicCall("Quit");
delete excel;
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QComboBox myCombo;
FB_test *test = new FB_test;
test->show();
excelTest();
return app.exec();
}
so here, instead of such right part
QString path = QString("C:\\databases\\oilnstuff.xls");
I want to get what QString FB_test::on_pushButton_clicked() returns.
How to make such thing?
UPD:
well, such way not works
QString mypath = test->ui.pushButton();
P.S.
Oh and btw, I am not sure about the way I degin this module.
Maybe I should move all working stuff from main.cpp into FB_test.cpp and get what button returns there and only call show() in main.cpp?

You could emit a signal, that a new path was selected and connect it to another component's slot for loading this file. With this, you can get a loose coupling between classes.
Signals are like:"Hey, something has happended. But I don't matter about who cares about that".
Slots are like: "I do that work, but I don't care about who wants me to do that".
fb_test.h
class FB_test : public QMainWindow
{
Q_OBJECT
public:
FB_test(QWidget *parent = 0, Qt::WFlags flags = 0);
~FB_test();
signals:
void newPathSelected(const QString& path);
private:
Ui::FB_testClass ui;
public slots:
void on_pushButton_clicked();
};
fb_test.cpp
// ...
void FB_test::on_pushButton_clicked()
{
QString path;
path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null);
if (!path.isEmpty())
emit newPathSelected(path);
}
An an excel class could look like this:
class MyExcelClass : public QObject
{
Q_OBJECT
public:
MyExcelClass(QObject *parent = 0);
~MyExcelClass();
public slots:
void readExcelSheet(const QString& path);
};
If you have instances of both classes in your main.cpp, this file would look like this:
int main(int argc, char** argv)
{
QApplication app(argc, argv);
FB_test fbTest;
MyExcelClass *excelClass = new MyExcelClass(&fbTest);
connect(&fbTest, SIGNAL(newPathSelected(QString)),
excelClass, SLOT(readExcelSheet(QString));
return app.exec();
}
Note that fbTest in main isn't dynamically allocated with new because this will result in a memory leak when main finishes. The excelClass object will get a pointer to the fbTest object in its constructor, this will ensure, that excelClass will get deleted as child object of fbTest.
If both the signal and the slot would be in the same class, you have to connect these in the constructor like this:
connect(this, SIGNAL(newPathSelected(QString)),
this, SLOT(readExcelSheet(QString));

Related

Is it normal not to be able to reuse a variable obtained from a connect()?

I made a login page and I'm trying to retrieve the name of the user who logged in and then display it in the next page and to adjust my program according to the person logged in.
But I desperately run into the same problem, I can't extract the variable from the slot to reuse it and do everything I have to do with it :(
So I wonder if it's a specificity of the slot (I start Qt and I don't know much about it) or if I'm the one who doesn't do things correctly.
PS: I've obviously read and reread the Qt documentation about this but it doesn't help too much.
My code:
glybook.cpp (where I want to put the login information)
#include "ui_glybook.h"
glybook::glybook(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::glybook)
{
ui->setupUi(this);
//qDebug() << receiveConnectionData(username);
test();
}
void glybook::receiveConnectionData(QString name){
username = name;
//qDebug() << username;
ui->label->setText("Connected: "+username);
}
void glybook::test(){
qDebug() << ui->label->text();
}
glybook::~glybook()
{
delete ui;
}
glybook.h
class Glybook;
}
class Glybook : public QMainWindow
{
Q_OBJECT
public:
explicit Glybook(QWidget *parent = nullptr);
~Glybook();
void test();
private slots:
void receiveConnectionData(QString);
private:
Ui::Glybook *ui;
QString username;
};
connection.cpp (login page)
#include "./ui_connection.h"
Connection::Connection(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Connection)
{
ui->setupUi(this);
}
Connection::~Connection()
{
delete ui;
}
void Connection::on_pushButton_clicked()
{
QString user = ui->lineEdit->text();
QString pass = ui->lineEdit_2->text();
glybook* page = new glybook();
connect(this, SIGNAL(sendConnectData(QString)), page, SLOT(receiveConnectionData(QString)));
emit sendConnectData(user);
page->show();
this->close();
}
connection.h
#define CONNECTION_H
#include <QMainWindow>
#include <QMessageBox>
#include "glybook.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Connection; }
QT_END_NAMESPACE
class Connection : public QMainWindow
{
Q_OBJECT
public:
Connection(QWidget *parent = nullptr);
~Connection();
private slots:
void on_pushButton_clicked();
signals:
void sendConnectData(QString);
private:
Ui::Connection *ui;
};
#endif // CONNECTION_H
main.cpp
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Connection w;
w.show();
return a.exec();
}
Cause
You call the test method of glybook in its constructor, which is executed as a result of this line:
glybook* page = new glybook();
At that time neither the connection
connect(this, SIGNAL(sendConnectData(QString)), page, SLOT(receiveConnectionData(QString)));
is made, nor the signal sendConnectData is emitted, so your qDebug() << ui->label->text(); prints an empty QString.
Solution
If you want to see the value sent by Connection in glybook, check it AFTER the execution of the receiveConnectionData slot.
That being said, I highly suggest you to read about Object Oriented Programming, as well as to go through a well known Qt course by VoidRealms on Youtube.

Qt: slots are not being called from the second thread

I want to update UI from the second thread, I have created slots which are going to be called from other thread by signaling, but somehow it is not being called from the other thread. Below is the code:
WorkerThread.h
class WorkerThread: public QObject
{
Q_OBJECT
public:
WorkerThread();
~WorkerThread();
public slots:
void onStart();
signals:
void sendMessage(const QString& msg, const int& code);
};
WorkerThread.cpp
#include "workerwhread.h"
WorkerThread::WorkerThread(){}
void WorkerThread::onStart(){
emit sendMessage("start", 100);
}
Usage:
MyWidget.h
namespace Ui {
class MyWidget;
}
class MyWidget: public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
private slots:
void onGetMessage(const QString &msg, const int& code);
private:
Ui::MyWidget *ui;
QThread *thread = nullptr;
WorkerThread *wt = nullptr;
};
MyWidget.cpp
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget)
{
ui->setupUi(this);
wt = new WorkerThread;
thread = new QThread;
connect(thread, &QThread::finished, wt, &QObject::deleteLater);
connect(ui->btStart, &QPushButton::clicked, wt, &WorkerThread::onStart);
connect(wt, &WorkerThread::sendMessage, this, &MyWidget::onGetMessage);
wt->moveToThread(thread);
thread->start();
}
void MyWidget::onGetMessage(const QString &msg, const int& code)
{
qDebug() << "message" << msg; // this never being called ????
}
Note: When I pass the connection type Qt::DirectConnectoin, then it is working, but the problem is it is not the GUI thread.
connect(wt, &WorkerThread::sendMessage, this, &MyWidget::onGetMessage, Qt::DirectConnection);
Main
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.setWindowIcon(QIcon(":/icons/system.png"));
return a.exec();
}
After a lot of trying and checking the code line by line, I finally found the problem. The reason was with overriding the event() function of QWidget, the return value of event() function is bool, so if you return a true it is OK and working well without throwing any runtime or compile-time error. But it will prevent signal-slot events to happen.
So NOT return true, but return QWidget::event(event); then it will slove the problem.

my thread is not working well it gives all result, together at last not one by one & GUI is got hanged during thread run?

I want to search files by name in a particular location as selected by the user. I want that as soon as I got the file. It must be put in QTreeWidget parallely and showing a QMovie(":/images/img_searching.gif") while searching is in progress until user did not stop searching.
ThreadSearch.h
#ifndef QTHREADSEARCH_H
#define QTHREADSEARCH_H
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QFileInfoList>
class QThreadSearchFileName : public QThread
{
Q_OBJECT
public:
QThreadSearchFileName(QObject *parent = 0);
~QThreadSearchFileName();
void run();
void getAllfiles(QStringList, QDir);
signals:
void fileInfoList(QFileInfo);
private:
QMutex m_Mutex;
QWaitCondition m_WaitCondition;
};
#endif
ThreadSearch.cpp
#include "ThreadSearch.h"
#include <QApplication>
#include <QThread>
QThreadSearchFileName::QThreadSearchFileName(QObject *parent):QThread(parent)
{
}
QThreadSearchFileName::~QThreadSearchFileName()
{
m_WaitCondition.wakeOne();
wait();
}
void QThreadSearchFileName::run()
{
QMutexLocker locker(&m_Mutex);
}
void QThreadSearchFileName::getAllfiles(QStringList targetStrList, QDir currentdir)
{
for(long int i1=0; i1<targetStrList.size(); i1++)
{
QString targetStr;
targetStr = targetStrList[i1];
QDirIterator it(currentdir, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString filename = it.next();
QFileInfo file(filename);
if (file.isDir())
{ // Check if it's a dir
continue;
}
if (file.fileName().contains(targetStr, Qt::CaseInsensitive))
{
emit fileInfoList(file);
}
}
}
}
main.cpp
#include <QCoreApplication>
#include <QDebug>
#include <QDirIterator>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QThreadSearchFileName *m_pSearchFileNameThread = new QThreadSearchFileName;
for(int i=0; i<userSelectedpathList.size(); i++)
{
QDir dir(userSelectedpathList[i]);
m_pSearchFileNameThread ->getAllfiles(stringListToBeSearch, dir);
connect(m_pSearchFileNameThread,SIGNAL(fileInfoList(QFileInfo)),this,SLOT(searchFileNameResult(QFileInfo)));
}
return a.exec();
}
void main::searchFileNameResult(QFileInfo file1) //Now Making SearchFile Name Tree
{
QTreeWidgetItem *SearchTreeItem = new QTreeWidgetItem(m_psearchProgresswdgt->finalSearchList_treeWidget);
SearchTreeItem->setCheckState(0,Qt::Unchecked);
SearchTreeItem->setText(1,file1.baseName());
}
It is good practice to separate such operations out from GUI object. What is more I would suggest higher-level async mechanism provided by QObject:
Make some class which could handle searching, for example
SearchingClass:
class SearchingClass : public QObject {
Q_OBJECT
public:
void setSomeSearchParametersOrSomething(QObject* something);
public slots:
void search();
signals:
void found(QObject* objectThatHasBeenFound);
}
Create instance of this class and move it into another thread:
auto searchingObject = new SearchingClass();
searchingObject->setSomeSearchParametersOrSomething(...);
auto thread = new QThread();
searchingObject->moveToThread(thread);
connect(this, SIGNAL(startSearchingSignal()), searchingObject, SLOT(search()));
connect(searchingObject, SIGNAL(found(QObject*)), this, SLOT(someHandleFoundSlot(QObject*)));
emit startSearchingSignal();
Make sure that found signal is being emitted every time that searching algorithm finds some result.
Ofc you must implement someHandleFoundSlot and declarate startSearchingSignal signal in GUI class.
I assume that you barely know Qt framework, so you should read about signals and slots as well as Qt meta-object system to fully understand whole code.
EDIT:
I see that you have edited your question. Your problem has several solution, I will describe you, what you did wrong comparably to what I had posted here.
Do not extend QThread. Extend QObject instead. It makes you can call moveToThread method. Crete an instance of QThread and pass it to this method. It causes later execution of slots to be performed in the thread you passed.
Do not make identical connections in loop until you want it to be executed more than once.
Make method getAllfiles (or search in my example) to be slot and do not call it manually. When you call method manually, it will always be performed in the same thread. Just connect it to some signal, and emit that signal.
[Just like you emit signal when you find matching file – the result is being handled in the slots objects thread.]
It's your decision, if you want to have thread for every single userSelectedpathList element. I would advice you to do it in one working thread (it's disc operations, I think it wouldn't be faster) and iterate that list inside getAllfiles method.
Previously I gave you generic answer about "how to do async work in Qt". Here's a simple implementation for your use case (I hope you will learn something).
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileInfo>
#include <QDirIterator>
#include <QThread>
#include <QDebug>
#include <QPushButton>
#include "filesearchingclass.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0){
setCentralWidget(&pushButton);
connect (&pushButton, SIGNAL(clicked(bool)), this, SLOT(main())); //invoke this->main() in event loop when button is pressed
qRegisterMetaType<QFileInfo>("QFileInfo");
searchingThread.start(); //thread must be started
}
~MainWindow(){
searchingThread.quit();
searchingThread.wait();
}
public slots:
void main() {
//EXAMPLE PARAMETERS:
QStringList pathList;
pathList.append("/home");
pathList.append("/var/log");
QStringList stringListToBeSearch;
stringListToBeSearch.append(".jpg");
stringListToBeSearch.append(".log");
//-------------------
auto fileSearchingObject = new FileSearchingClass(); //dynamic as you can't destroy object when it is out of scope
fileSearchingObject->moveToThread(&searchingThread); //important!!!
fileSearchingObject->setTargetStrList(stringListToBeSearch);
fileSearchingObject->setPaths(pathList);
connect(this,SIGNAL(startSearching()),fileSearchingObject,SLOT(search())); //do not call fileSearchingObject->search() manually
connect(fileSearchingObject,SIGNAL(foundFile(QFileInfo)),this,SLOT(searchFileNameResult(QFileInfo))); //handle every result in event loop
connect(fileSearchingObject, SIGNAL(searchFinished()), fileSearchingObject, SLOT(deleteLater())); //no need to wory about deleting fileSearchingObject now
emit startSearching(); //like calling fileSearchingObject->search() but in another thread (because of connection)
}
signals:
void startSearching();
public slots:
void searchFileNameResult(QFileInfo fileInfo) {
//do something
qDebug() << "---FOUND---" << fileInfo.absoluteFilePath() << "\n";
}
private:
QThread searchingThread;
QPushButton pushButton;
};
#endif // MAINWINDOW_H
filesearchingclass.h
#ifndef FILESEARCHINGCLASS_H
#define FILESEARCHINGCLASS_H
#include <QFileInfo>
#include <QDirIterator>
#include <QThread>
#include <QDebug>
class FileSearchingClass : public QObject {
Q_OBJECT
public:
~FileSearchingClass(){}
void setPaths (const QStringList &paths) {
userSelectedPathList = paths;
}
void setTargetStrList(const QStringList &value) {
targetStrList = value;
}
public slots:
void search() {
for(int i=0; i<userSelectedPathList.size(); i++) {
QDir currentDir(userSelectedPathList[i]);
for(long int i1=0; i1<targetStrList.size(); i1++)
{
QString targetStr;
targetStr = targetStrList[i1];
QDirIterator it(currentDir, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString filename = it.next();
QFileInfo file(filename);
if (file.isDir())
{ // Check if it's a dir
continue;
}
if (file.fileName().contains(targetStr, Qt::CaseInsensitive))
{
emit foundFile(file); //calling MainWindow::searchFileNameResult directly is possible, but bad idea. This thread is only focused in searching, not modifing widgets in GUI.
}
}
}
}
emit searchFinished(); //it's always good to know when job is done.
}
signals:
void foundFile(QFileInfo);
void searchFinished();
private:
QStringList userSelectedPathList;
QStringList targetStrList;
};
#endif // FILESEARCHINGCLASS_H

Share data between two QWidget instances

I would like share a string between two instances of QWidget.
In main.cpp, two objects are instantiated and shown like this:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w1,w2; //Derived from QWidget
w1.show();
w2.show();
return a.exec();
}
I would introduce SharedState class:
// shared_state.h
#ifndef SHARED_STATE_HPP
#define SHARED_STATE_HPP
#include <QObject>
class SharedState : public QObject
{
Q_OBJECT
public:
SharedState(QString initialValue = "")
: currentValue(initialValue)
{}
QString getCurrentValue()
{
return currentValue;
}
public slots:
void setValue(QString newValue)
{
if(currentValue != newValue)
{
currentValue = newValue;
emit valueChanged(currentValue);
}
}
signals:
void valueChanged(QString);
private:
QString currentValue;
};
#endif // SHARED_STATE_HPP
Now I would provide reference to SharedState in Dialog's constructor,
// dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QWidget>
#include "shared_state.h"
namespace Ui {
class Dialog;
}
class Dialog : public QWidget
{
Q_OBJECT
public:
explicit Dialog(SharedState& state, QWidget *parent = 0);
~Dialog();
private slots:
void handleTextEdited(const QString&);
public slots:
void handleInternalStateChanged(QString);
private:
Ui::Dialog *ui;
SharedState& state;
};
#endif // DIALOG_H
You may have noticed that I have added two slots, one to handle the case when the text is manually edited and one when shared state will inform us that we are out of date.
Now in Dialog's constructor I had to set initial value to textEdit, and connect signals to slots.
// dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(SharedState& state, QWidget *parent) :
QWidget(parent),
ui(new Ui::Dialog),
state(state)
{
ui->setupUi(this);
ui->textEdit->setText(state.getCurrentValue());
QObject::connect(ui->textEdit, SIGNAL(textEdited(QString)),
this, SLOT(handleTextEdited(QString)));
QObject::connect(&state, SIGNAL(valueChanged(QString)),
this, SLOT(handleInternalStateChanged(QString)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::handleTextEdited(const QString& newText)
{
state.setValue(newText);
}
void Dialog::handleInternalStateChanged(QString newState)
{
ui->textEdit->setText(newState);
}
Now the change in the main function:
// main.cpp
#include "dialog.h"
#include "shared_state.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SharedState state("Initial Value");
Dialog w1(state), w2(state);
w1.show();
w2.show();
return a.exec();
}

On Qt, how to change the icon of an action in the toolbar at runtime?

In a program which calculates abritary precision numbers.
I have an action on the taskbar.
QAction* button_stop_continue.
I've set the icon green icon in the beginning of the program, when calculations are being executed it should turn red and so on.
I've already tried something like this:
connect(this, SIGNAL(startedComputing()), this, SLOT(turnIconRed()));
connect(this, SIGNAL(finishedComputing()), this, SLOT(turnIconGreen()));
the function turnIconRed is looking similar to this:
void turnIconRed()
{
button_stop_continue->setIcon(QIcon("images/red-light.png"));
}
I've come up with some incredibly-ugly algorithms :S. Isn't there a straight-forward way to deal with that on Qt? Any ideas?
Thanks.
I would subclass QAction and add some logic for the states in which it can be. It is never a good idea to hardcode the color of something into the name of a method. By subclassing QAction, the look and feel of it is encapsulated.
This could be something like this:
Header file:
class StateAction : public QAction
{
Q_OBJECT
public:
explicit StateAction(QObject *parent = 0);
public slots:
void start();
void stop();
void pause();
};
Implementation file:
StateAction::StateAction(QObject *parent) :
QAction(parent)
{
this->stop();
}
void StateAction::start()
{
this->setIcon(QIcon(":/states/start.png"));
}
void StateAction::stop()
{
this->setIcon(QIcon(":/states/stop.png"));
}
void StateAction::pause()
{
this->setIcon(QIcon(":/states/pause.png"));
}
Now, in your MainWindow you can use that custom QAction simply by connecting its slots to the right signals:
Header file:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void startedComputing();
void finishedComputing();
void pausedComputing();
private:
void createActions();
void createToolbars();
void createConnections();
StateAction *m_stateAction;
};
Implementation file:
...
void MainWindow::createConnections()
{
connect(this, SIGNAL(startedComputing()), m_stateAction, SLOT(start()));
connect(this, SIGNAL(finishedComputing()), m_stateAction, SLOT(stop()));
connect(this, SIGNAL(pausedComputing()), m_stateAction, SLOT(pause()));
}
I've found a solution using QToolButton here it is:
Header : FenPrincipale.h
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QWidget>
#include <QVBoxLayout>
#include <QToolButton>
#include <QToolBar>
#include <QAction>
#include <QTextEdit>
class FenPrincipale : public QWidget {
Q_OBJECT
public:
FenPrincipale();
private slots:
void goComputing();
void stopComputing();
private:
QAction* actionDemarrer; // Start computing
QAction* actionArreter; // Stop computing
QToolBar* toolBarActions;
QToolButton* boutonAction; // this button holds the appropriate action
QVBoxLayout* layoutPrincipale;
QTextEdit* resultat; // show result
};
...
Implementation : FenPrincipale.cpp
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale() : QWidget()
{
this->setFixedSize(400, 200);
// create actions
actionDemarrer = new QAction(QIcon("bouton-vert.png"), "demarrer", this);
actionArreter = new QAction(QIcon("bouton-rouge.png"), "arreter", this);
boutonAction = new QToolButton;
boutonAction->setDefaultAction(actionDemarrer);
// create toolbar
toolBarActions = new QToolBar(this);
toolBarActions->addWidget(boutonAction);
// create result widget
resultat = new QTextEdit(this);
// create layout
layoutPrincipale = new QVBoxLayout(this);
layoutPrincipale->addWidget(toolBarActions);
layoutPrincipale->addWidget(resultat);
this->setLayout(layoutPrincipale);
// make connections
QObject::connect(actionDemarrer, SIGNAL(triggered()), this, SLOT(goComputing()));
QObject::connect(actionArreter, SIGNAL(triggered()), this, SLOT(stopComputing()));
}
void FenPrincipale::goComputing()
{
resultat->setText("Computing...");
boutonAction->setDefaultAction(actionArreter);
}
void FenPrincipale::stopComputing()
{
resultat->setText("Partial result : {?}");
boutonAction->setDefaultAction(actionDemarrer);
}
...
Main
#include <QApplication>
#include "FenPrincipale.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.show();
return app.exec();
}