Calling functions between classes
-
I have been trying to use a general log window for my application. I am new to QT and have had some luck calling the functions but it will not print the output to the textedit window. I have included the sample code below:
#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
namespace Ui {
class Dialog;
}class Dialog : public QDialog
{
Q_OBJECTpublic:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();private slots:
void on_pushButton_clicked();private:
Ui::Dialog *ui;
};#endif // DIALOG_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = nullptr);
~MainWindow();void MainLogEdit(QString);
private slots:
void on_pushButton_clicked();private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H#include <mainwindow.h>
#include "dialog.h"
#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}Dialog::~Dialog()
{
delete ui;
}void Dialog::on_pushButton_clicked()
{
QString message;
MainWindow *mui = (new MainWindow);
message = "ClassComm Message";
mui->MainLogEdit(message);}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textBrowser->append("Welcome");
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::MainLogEdit(QString message)
{
ui->textBrowser->append(message);
}void MainWindow::on_pushButton_clicked()
{
QString message = "This Is a Test!!! ";MainLogEdit(message); // this->close(); Dialog sdialog; sdialog.setModal(true); sdialog.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}I know there must be an easy way.
Thanks
Dennis Clark -
Good, then please mark the thread as solved so other forum users may know a solution has been found :-)
-
Try to understand what you are doing.
In main you create a MainWindow and show it:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
then in Dialog::on_pushButton_clicked() you create another one !
void Dialog::on_pushButton_clicked() { QString message; MainWindow *mui = new MainWindow; message = "ClassComm Message"; mui->MainLogEdit(message); }
add mui->show() at the end
and you will see the message but in the wrong window :) -
-
I see it creates a new window and shows display it, but it opens multiple windows. :( Is there a way to update the current Main Window? I understand now what is happening. How do I access the my MainLogEdit() function from the dialog class with out creating mui???
-
@dencla said in Calling functions between classes:
but it opens multiple windows
THIS IS EXACTLY WHAT @mpergand TOLD YOU!
Do not create a new MainWindow in the slot.
If you want to send something to MainWindow from Dialog create a signal in Dialog and connect a slot to that signal in MainWindow. Then in Dialog you emit this signal passing it the message. This is how Qt applications work.
https://doc.qt.io/qt-5/signalsandslots.html -
Use signal and slot (basic Qt programming)
Emit a signal from your dialog that will be received in a slot of your MainWindow. -
@dencla said in Calling functions between classes:
but how do I call the MainLogEdit function in MainWindow with emit() from Dialog.
You don't call anything, you emit a signal.
Have you defined a signal in Dialog.h
and a slot in MainWindow.hShow us your code.
-
@dencla said in Calling functions between classes:
Yes it is at the top of the post.
No, it is not.
Again: add a signal to your dialog, add a slot to MainWindow, connect both, in your dialog you emit this signal. -
@dencla
@jsulm referred you earlier to https://doc.qt.io/qt-5/signalsandslots.html doc page. There is a heading there A Small Example:A minimal C++ class declaration might read:
That example shows the components you need for @mpergand's
Emit a signal from your dialog that will be received in a slot of your MainWindow.
- Declaration of a signal method. (
Dialog.h
) emit
of the signal. (Dialog.cpp
)- Declaration of a slot method. (
MainWindow.h
) connect()
of the signal-object+signal-method to the slot-object+slot-method. (MainWindow.cpp
)
That is what you need to write. The only difference for you is where in the simple example has both signal & slot in the same class, you will have them in separate classes. 1 & 2 above are to be in your
Dialog
class, 3 & 4 inMainWindow
class. - Declaration of a signal method. (
-
@JonB That is what I am working on I have been watching videos and such to do what you are suggesting. I understand the concept, but I'm sure that it is the syntax that is throwing me. Thanks to all. SOmetimes it takes a lot to get a new idea through to an old brain.
-
I am understanding the issue that I am having thanks to everyone's help. I am still having an issue with the dialog pushbutton emiti correctly to the Main Window. Below is the code in the MainWindow.cpp the signals and slots are in their prespective header files. The code compiles but does not generate a message in MainWindow textEdit window from the onDataChange signal, but it does for the onNewData signal. I am close but still cannot get the window to update.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
Dialog test;ui->setupUi(this); ui->textBrowser->append("Welcome"); QString text = "Comm Test"; connect(this,&MainWindow::onNewData, this,&MainWindow::MainLogEdit); connect(&test,&Dialog::onDataChange, this,&MainWindow::MainLogEdit);
}
MainWindow::~MainWindow()
{
delete ui;
}int counter;
void MainWindow::MainLogEdit(QString message)
{
ui->textBrowser->append(message);
ui->lineEdit->setText(QString::number(counter++));}
void MainWindow::on_pushButton_clicked()
{
QString message = "This Is a Test!!! ";
emit onNewData(message);
Dialog sdialog;
sdialog.setModal(true);sdialog.exec();
}
I will continue exploring what is needed. It is my guess that the Diaglog test is another instance of Dialog.
Thanks again. -
Hi,
At no point are you connecting the dialog to your main window.
-
So you also updated the connections ?
Can you now properly communicated between the dialog and the main window ?