Linking a QWidget to another QWidget new created - c++

I am try to, by pressing a button in the main QWidget, to create a new QWidget. In that new created QWidget I want to have a button connected to a slot of the main QWidget.
class UI : public QWidget
{
public:
UI(){connection();};
private:
QPushButton* all = new QPushButton{ "ALL" };
void connection(){
QObject::connect(all,QPushButton::clicked,[](){
SmallGUI* s=new SmallGUI{};
s->show();
});
}
void something(){
//something
}
and the second class
class SmallGUI1 :
public QWidget
{
public:
SmallGUI(){connection();};
private:
QPushButton* N =new QPushButton;
void connection(){
//to connect N to something()
}
I want to connect N to something() .

Before we start, there are some other problems with you code.
Note that in your second class, the constructor is not named the same as the class, which will cause some... Problems.
You also forgot to put a parent for your buttons (which may thus cause some unexpected results) AND for your Widgets (which is again not a good idea).
So, that being said, let us get to the main topic.
I tend to only put prototypes and declare the attributes in the .h file to make the code clearer, but you may of course adapt it to your needs or to your own programming convention.
There are several ways to do something like this, but the simplest one should look like this :
SmallGUI1.h :
#include "UI.h" //The file in which the class UI is declared
//OR :
//class UI; //If you need to include this file in UI.h
class SmallGUI1 : public QWidget{
Q_OBJECT //Q_OBJECT macro, check the doc for more explainations about it
public:
explicit SmallGUI1(UI *parent = nullptr); //Explicit means that this constructor cannot be used for implicit casts
~SmallGUI1();//Destructor needed because I only put the constructor above
private:
QPushButton* N; //Not very good looking to initialize attributes in the .h in my opinion, but works fine.
}
SmallGUI1.cpp :
SmallGUI1::SmallGUI1(UI *parent) : QWidget(parent){
N = new QPushButton(tr("Some text on the button") , this); //tr to enable translation on this string
//************* If I understood your question correctly, this is what you are looking for *************
connect(N , &QPushButton::clicked , parent , &UI::doSomething); //Select the signal you want
/*
Some code here
*/
show();
}
SmallGUI1::~SmallGUI1(){qDeleteAll(children());}
UI.h :
class UI : public QWidget{
Q_OBJECT
public:
explicit UI(QWidget *parent = nullptr);
~UI();
private:
QPushButton* all;
private slots :
void createSmallGUI1();
void doSomething();
}
UI.cpp :
#include "SmallGUI1.h"
UI::UI(QWidget *parent) : QWidget(parent){
all = new QPushButton(tr("ALL") , this);
connect(all , &QPushButton::clicked , this , &UI::createSmallGUI1);
/*
Some code here
*/
}
UI::~UI(){qDeleteAll(children());}
void UI::createSmallGUI1(){SmallGUI1 *gui = new SmallGUI1(this);}
void UI::doSomething(){
/*
Clever code here
*/
}

You can define the second widget as a child of the main widget to make things easier:
class UI : public QWidget {
...
private:
SmallGUI* s;
...
and then initialize it in the UI constructor, along with your all button. You can initially hide the child widget or disable it:
UI() {
all = new QPushButton{"ALL", this};
setWindowTitle("UI"); // just for clarification
s = new SmallGUI(this);
s->hide();
connection();
};
and 'show' it with button clicked signal
connect(all, &QPushButton::clicked, s, &SmallGUI::show);
Doing so gives you the option to connect the clicked signal of your N button to the something function in the parent class
connect(s->N, &QPushButton::clicked, this, &UI::something);
The complete program would be as follows,
#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
#include <QWidget>
class SmallGUI : public QWidget {
public:
SmallGUI(QWidget* parent) : QWidget(parent) {
N = new QPushButton{"btn2", this};
connection();
};
QPushButton* N;
private:
void connection(){};
};
class UI : public QWidget {
public:
UI() {
all = new QPushButton{"ALL", this};
setWindowTitle("UI"); // just for clarification
s = new SmallGUI(this);
s->hide();
connection();
};
private:
SmallGUI* s;
QPushButton* all;
void connection() {
connect(all, &QPushButton::clicked, s, &SmallGUI::show);
connect(s->N, &QPushButton::clicked, this, &UI::something);
}
void something() { QMessageBox::information(this, "Hello", "Hello"); }
};
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
UI w;
w.show();
return a.exec();
}

It is not good idea to connect to parent's slots from "nested" class, since SmallGUI1 will be tied to class UI.
Here is better solution, I think:
class UI : public QWidget
{
public:
UI(){connection();};
private:
QPushButton* all = new QPushButton{ "ALL" };
void connection(){
QObject::connect(all,QPushButton::clicked,[](){
SmallGUI1* s=new SmallGUI1;
connect(s,&USmallGUI1::button_clicked,this,&UI::something);
s->show();
});
}
void something(){
//something
}
And SmallGUI1 class:
class SmallGUI1 :
public QWidget
{
public:
SmallGUI1(){connection();};
signals:
void button_clicked();
private:
QPushButton* N;
void connection(){
//to connect N to something()
N = new QPushButton;
connect(N,&QPushButton::clicked,this,&SmallGUI1::button_clicked)
}
This way, you are connecting QPusButton::clicked signal from SmallGUI1 to the signal SmallGUI1::button_clicked(). Dont need to implement additional slot, just connect signal to signal.
And in UI you are connecting button_clicked() signal to the slot dosomething()
DONT FORGET THE CONSTRUCTOR OF SmallGUI1! In your code, SmallGUI() will be just a method, which will not be called when SmallGUI1 is instantiated, and you have to call it by yourself.

Related

How to access QMainWindow from another class

// mainwindow.h
#include "ui_MainWindow.h"
#include "parseTextFile.h"
class MainWindow:public QMainWindow
{
Q_OBJECT
public:
MainWindow(void);
private:
Ui_mainWindow ui;
parseFile *fileParse;
public slots:
int onOkButtonClick();
};
// mainwindow.cpp
MainWindow::MainWindow(void)
{
ui.setupUi(this);
connect(ui.OkButton,SIGNAL(clicked()),this,SLOT(onOkButtonClick()));
}
int MainWindow::onOkButtonClick()
{
fileParse = new parseFile(this);
fileParse->parseTextFile();
return 0;
}
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
MainWindow *mainWindow = new MainWindow();
mainWindow->show();
return app.exec();
}
// parseTextFile.h
class parseFile
{
public:
parseFile(QWidget *parent =0);
~parseFile();
int parseTextFile( );
};
// parseTextFile.cpp
#include "parseTextFile.h"
#include <QMessageBox>
parseFile::parseFile(QWidget *parent)
{
}
parseFile::~parseFile()
{
}
int parseFile::parseTextFile( )
{
QMessageBox::information(this,"a","b");
return 0;
}
I can able to access parseTextFile method but i am getting error in QMessageBox. Is it the right way?
How to print QMessageBox in parseTextFile class?
anything needs to add in parseTextFile constructor?
Pass this to QMessageBox::information as a parent is not a valid argument because parseFile is not a QWidget derived class.
For simple, just pass nullptr instead of this :
QMessageBox::information(nullptr ,"a","b");
In this case, the message box belongs to no one. It's will be closed by the user or when the application exits.
PS: you should release the memory after using fileParse to avoid memory leaks.
There are several ways to like with the main window (static pointer, singleton, top widgets enumeration,etc.) but the simplest one, that I thinks you intend to do, is to make parseFile class as QObject derived class and QMainWindow its parent.
class parseFile: public QObject
{
Q_OBJECT
public:
parseFile(QWidget *parent =0);
~parseFile();
int parseTextFile();
};
parseFile::parseFile(QWidget *parent)
:QObject(parent)
{
}
int parseFile::parseTextFile()
{
QWidget * parentWidget = qobject_cast<QWidget *>( parent() ); //<-- QMainWindow instance
QMessageBox::information( parentWidget, "a", "b");
return 0;
}
int MainWindow::onOkButtonClick()
{
fileParse = new parseFile(this);
fileParse->parseTextFile( );
//fileParse should be released by:
//fileParse->deleteLater();
return 0;
}

C++, QT. Function call for QShortcut does not exist

I have a class, MainWindow with a pointer to a Canvas, mCanvas...
mainwindow.h...
#include "canvas.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
//snip
private:
Canvas* mCanvas;
};
canvas.h...
class MainWindow;
class Canvas
{
public:
Canvas(MainWindow* parent);
//snip
public slots:
void doSomething();
private:
MainWindow* mParent;
};
Canvas.cpp...
Canvas::Canvas(MainWindow* parent)
{
mParent = parent;
}
void Canvas::doSomething()
{
//snip
}
In MainWindow.cpp, within the MainWindow constructor, I point mCanvas to an Canvas(this). I then attempt to create a new QShortcut with the action Canvas::doSomething().
MainWindow.cpp...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
new QShortcut(QKeySequence(Qt::Key_BracketLeft),
mCanvas, SLOT(doSomething()) );
However, when I attempt to run the program, I recieve the error NO MATCHING FUNCTION CALL for doSomething(), meaning the compiler does not think that doSomething() exists. In the new QShortcut, I have written mCanvas as mCanvas, *mCanvas, &mCanvas; nothing works.
What is going wrong?
To use signals and slots in Canvas you need to inherit it from QObject (from QWidget in your case) and use Q_OBJECT macro. Also your mCanvas is not initialized before using in new QShortcut ....
Or do something like this:
auto shortcut = new QShortcut(QKeySequence(Qt::Key_BracketLeft), this);
QObject::connect(shortcut, &QShortcut::activated, this, [ this ] { mCanvas->doSomething(); } );

Reference in C++ classes

I'm new to programming. I can not understand how to make a reference to the methods of another class.
I have several files and classes:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore/QtGlobal>
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class Valve;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void openValve(int id);
void closeValve(int id);
private:
Ui::MainWindow *ui;
Settings *settings;
Valve *valve;
};
class A {
...
private:
void start();
}
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowFlags(Qt::CustomizeWindowHint);
this->setFixedSize(this->geometry().width(),this->geometry().height());
//класс для 7 клапанов
valve = new Valve(7);
}
MainWindow::~MainWindow()
{
delete settings;
delete ui;
}
void MainWindow::valveSwitch(int id)
{
if (valve->getState(id))
closeValve(id);
else
openValve(id);
}
void MainWindow::openValve(int id)
{
QString str = "Valve №" + QString::number(id);
valveButton[id-1]->setEnabled(false);
if (valve->open(id)) {
valveButton[id-1]->setEnabled(true);
//valveButton[id-1]->setPalette(QPalette(Qt::green));
//valveButton[id-1]->setStyleSheet(VALVE_OPEN_COLOR);
QString style = QString(DEFAULT_STYLE_BUTTON) + QString(DEFAULT_BACKGROUND_BUTTON);
valveButton[id-1]->setStyleSheet(style);
ui->mainLabel->setText(str + " open! :)");
}
else {
valveButton[id-1]->setEnabled(true);
ui->mainLabel->setText("Cant open " + str);
remoteDisconnect();
}
}
void MainWindow::closeValve(int id)
{
QString str = "Valve №" + QString::number(id);
valveButton[id-1]->setEnabled(false);
if (valve->close(id)) {
valveButton[id-1]->setEnabled(true);
//valveButton[id-1]->setPalette(style()->standardPalette());
valveButton[id-1]->setStyleSheet("");
ui->mainLabel->setText(str + " close! :)");
}
else {
valveButton[id-1]->setEnabled(true);
ui->mainLabel->setText("Cant close " + str);
remoteDisconnect();
}
}
A::A
{
}
A::~A
{
}
void A::start()
{
//MainWindow::openValve(2);
//valve.open(3);
}
How do I access MainWindow class methods openValve/closeValve from class A?
Or how can I access an instance valve of a class Valve of MainWindow's constructor from class A?
//MainWindow::openValve(2);
//valve.open(3);
At very first:
openValve is not static, so you need an instance of MainWindow to be able to call it:
MainWindow* mw_ex0;
// alternatively, if more appropriate:
MainWindow& mw_ex1;
mw_ex0->openValve(2);
mw_ex1.openValve(2);
The MainWindow instance could be a parameter of your function start or a member variable of class A – depending on your concrete needs.
Same applies if you want to access the valve member (valve is a pointer, so you need operator->): mw_ex0->valve->open(3); or mw_ex1.valve->open(3);).
However, you need to grant class A access to those currently private members; three options:
Make A a friend class of MainWindow - this allows A to access MainWindow's private members (might apply for Valve class, too, if open is not public).
Make the appropriate functions public (MainWindow::openValve and Valve::open); to access the valve member of MainWindow, too, you could make it public, too, but it is in general not recommendable to make the internals of a class publicly available to the outside world - someone might simply change your valve member to something else - and your program is broken... So rather provide a simple getter for.
Make A an inner class of MainWindow. Then it gets access to its outer class members implicitly (depending on the requirements for class A, this might not be suitable – up to you to decide...).
Sidenotes:
In your constructor, you do not initialise the settings member.
You do not clean up the valve member in your destructor (potential memory leak).
To avoid having to clean up, you could to incorporate valve directly in your class - this is not always suitable, but might be a good option here (up to you to decide, just showing the alternative):
class MainWindow
{
Valve valve;
};
MainWindow::MainWindow()
: valve(7) // calls constructor directly
{ }
Be aware that you now do use operator. to access the valve's members (mw_ex0->valve.open(3);). Advantage is that Valve will no be automatically cleaned up together with MainWindow. Alternatively, a std::unique_ptr could be used to hold the pointer to your Valve instance, then you get automatic cleanup, too.
You should pass a MainWindow object into the A::start method:
class A {
...
private:
void start(MainWindow & w);
}
void A::start(MainWindow & w) {
w._MainWindow_method_name_here_();
}
Or you should declare a static method in MainWindow class:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
static void aStaticMethod();
};
void MainWindow::aStaticMethod() {
...
}
void A::start() {
MainWindow::aStaticMethod();
}
To access protected/private methods of MainWindow you should declare the A class as a friend of MainWindow:
class MainWindow : public QMainWindow
{
friend class A;
...
};
Update
I create a new class for it to work in a separate thread, and call its methods from the main class (by clicking on the button). Accordingly, I need class A to open / close valves, etc.
The "true Qt way" is to use signals & slots mechanism.
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// Transform open/closeValve methods into slots
// (a method that can be assigned as an event handler)
//
public slots:
void openValve(int id);
void closeValve(int id);
private:
Ui::MainWindow *ui;
Settings *settings;
Valve *valve;
};
// This class should be a descendant of QObject
//
class A : public QObject
{
Q_OBJECT
// Transform this method to a slot, so it can be called
// as regular method, or can be assigned as an event handler,
// for instance, as QPushButton::click handler.
//
public slots:
void start();
// Add signals
//
signals:
void openValveSignal(int id);
void closeValveSignal(int id);
}
void A::start()
{
// do something and then emit the signal to open valve,
// MainWindow::openValve(2) will be called
emit openValveSignal(2);
...
// do something and then emit the signal to close valve,
// MainWindow::closeValve(3) will be called
emit closeValveSignal(3);
}
// connects A signals with MainWindow slots,
// so when you `emit A::***Signal()` then corresponding
// `MainWindow::***` method will be called
//
void initialize(MainWindow * pWnd, A * pA)
{
QObject::connect(pA, &A::openValveSignal, pWnd, &MainWindow::openValve);
QObject::connect(pA, &A::closeValveSignal, pWnd, &MainWindow::closeValve);
}
You can call a->start() method from MainWindow methods as usual. Or you can connect button clicked signal with A::start method, for instance:
void initialize(MainWindow * pWnd, QAbstractButton * pBtn, A * pA)
{
// C++11 lambda function is used here because A::start has no arguments
QObject::connect(pBtn, &QAbstractButton::clicked, [pA](){ pA->start(); });
QObject::connect(pA, &A::openValveSignal, pWnd, &MainWindow::openValve);
QObject::connect(pA, &A::closeValveSignal, pWnd, &MainWindow::closeValve);
}
so when you click a button then A::start method will be called automatically. And then MainWindow::open/closeValve methods will be called from A::start method.
Declare openValve as public method and valve as public object (open must be public too)
Then use as:
MainWindow mainWindow;
mainWindow.openValve(2);
mainWindow.valve.open(3);

Mapping a Qt base class signal to a slot in a derived class

I am having a problem with Qt signals and slots. I am just learning Qt but I have lots of C++ experience. I have derived a class from QTreeView and I want to handle the columnResized signal. The slot is never being called and I am seeing this in the 'Application Output':
QObject::connect: No such signal TRecListingView::columnResized(int,int,int) in ../ec5/reclistingwidget.cpp:142
The class declaration looks like this:
class TRecListingView : public QTreeView
{
Q_OBJECT
public:
TRecListingView(QWidget *parent, TTopicPtr topic);
~TRecListingView();
private slots:
void onColumnResized(int index, int oldsize, int newsize);
private:
TRecListingModel *Model = 0;
};
In the constructor I am doing this:
connect(this,SIGNAL(columnResized(int,int,int)),
this,SLOT(onColumnResized(int,int,int)));
I had this working earlier before I implemented the derived class. Then I was mapping the signal to a slot in the parent widget.
I have tried running qmake and rebuilding the project. I also tried this:
QTreeView *tv = this;
connect(tv,SIGNAL(columnResized(int,int,int)),
this,SLOT(onColumnResized(int,int,int)));
columnResized is not a signal, but slot, so you cannot connect to it.
Instead you can connect to the QHeaderView::sectionResized
connect(this->horizontalHeader(),SIGNAL(sectionResized(int,int,int)),
this, SLOT(onColumnResized(int,int,int)));
Because it is not a signal:
From documentation:
void QTreeView::columnResized ( int column, int oldSize, int newSize ) [protected slot]
Try reimplement it:
#include <QTreeView>
#include <QHeaderView>
#include <QTimer>
#include <QDebug>
class TRecListingView : public QTreeView
{
Q_OBJECT
public:
TRecListingView(QWidget *parent=0):
QTreeView(parent)
{
QTimer::singleShot(0, this, SLOT(fixHeader()));
}
public slots:
void fixHeader()
{
QHeaderView *hv = new QHeaderView(Qt::Horizontal, this);
hv->setHighlightSections(true);
this->setHeader(hv);
hv->show();
}
protected slots:
void columnResized(int a, int b, int col)
{
qDebug() << "This is called";
}
public slots:
};
Simple usage:
TRecListingView trec;
QStringList stringList;
stringList << "#hello" << "#quit" << "#bye";
QStringListModel *mdl = new QStringListModel(stringList);
trec.setModel(mdl);
trec.show();
Now it works properly and when you resize header, you'll see many This is called strings.

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