second window controls mainwindow QtSerialPort dialog
-
hello everyone I'm new, I'm studying from 4 days qt and do not know English so I apologize.
I have created a mainwindow with a text label and a button (1), which opens a second window.
in the second window I have another button (2) and would like to change the text of the label of the first window.This serves as an example because I'm building in my program a window with settings that change aspects of the first window as the text color and more ...
I thank you all hope to avoid mistakes
//===========================================
WindowSetup = new windowsetup(this);
WindowSetup->show(); -
@ilmandorlone
please help.
I attach here the code of the second window where I want to change the label 1<code>
#include "secondwindow.h"
#include "ui_secondwindow.h"
#include "QDialog"secondwindow::secondwindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::secondwindow)
{
ui->setupUi(this);
}secondwindow::~secondwindow()
{
delete ui;
}void serialsetup::on_pushButton_clicked()
{
//MAINWINDOW->ui->label->setText("hello");
}</code>
-
Two different classes should not access their ui members directly. Instead provide some access methods e.g.
MainWindow::setText(const QString& text)
and call that.The other thing is that you should avoid creating mutual dependencies i.e. if main window creates the second window then second window should not access main window because this creates a circular dependency.
Instead make the second window emit a signal and connect to it in the main window so that the second window has no knowledge (and no #include) of the main window. -
ok. thanks a lot.
I tried all day for a solution, trying not to disturb more on the forum.
unfortunately nothing, and I ask you an example to help me.I have created a second window for the settings between here many settings to QtSerialPort.
when the user selects different settings and ends the dialog with (apply button) the first window captures the new settings.I ask a small simple example where I can figure out how to proceed.
I have created a basis for an example, this is MyDialog.cpp
//================
#include "mydialog.h"
#include "ui_mydialog.h"MyDialog::MyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyDialog)
{
ui->setupUi(this);
}MyDialog::~MyDialog()
{
delete ui;
}void MyDialog::on_pushButton_clicked()
{
// =======================================================================
// my Button_change_text_mainwindow or for example change serial port ....
} -
As I said - the dialog should not reach to main window. It should not have any knowledge of main window. It could for example accept a settings pointer and play with that. A short example that you can copy paste to test it out:
#include <QApplication> #include <QDialog> #include <QPushButton> #include <QLineEdit> #include <QVBoxLayout> struct Settings { QString stuff = "Default value"; }; class Dialog: public QDialog { public: Dialog(QWidget* parent = nullptr) : QDialog(parent) { setLayout(new QVBoxLayout()); layout()->addWidget(editor = new QLineEdit(this)); layout()->addWidget(okBtn = new QPushButton("OK", this)); connect(okBtn, &QPushButton::clicked, this, &QDialog::accept); } void editSettings(Settings* s) { settings = s; if(settings) { editor->setText(settings->stuff); if(exec() == QDialog::Accepted) settings->stuff = editor->text(); } } private: Settings* settings = nullptr; QLineEdit* editor = nullptr; QPushButton* okBtn = nullptr; }; class MainWindow : public QWidget { public: MainWindow(QWidget* parent = nullptr) : QWidget(parent) { auto btn = new QPushButton("Edit settings", this); connect(btn, &QPushButton::clicked, [&]{ Dialog dialog; dialog.editSettings(&settings); }); setLayout(new QVBoxLayout()); layout()->addWidget(btn); } private: Settings settings; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
This should be split into proper header/cpp files of course.