Access MainWindow class members from another class - c++

I googled a lot before writing here. I found a couple of posts but I couldn't quite get them. So I'm starting a new post.
I am developing a simple UI. There is this main class (see below), which is a main dialogue box which is shown when the app is started.
class MainWindow : public QMainWindow
There are few buttons on the MainWindow dialogue box, when I click a button, it hides the MainWindow and opens another QDialog box. This dialogue box is of the class as shown below
class libinsert : public QDialog
I create the libinsert object this way:
void MainWindow::on_pushButton_clicked()
{
this->hide();
libinsert lib; // I create libinsert object this way
lib.setModal(true);
lib.exec();
}
This is libinsert.cpp:
libinsert::libinsert(QWidget *parent) :
QDialog(parent),
ui(new Ui::libinsert)
{
ui->setupUi(this);
}
When the libinsert dialogue box is open, I need to access a socket variable which is a member of class MainWindow. How do I achieve this ?
My main app looks like this:
{
QApplication a(argc, argv);
MainWindow w(sock);
w.show();
return a.exec();
};
Alternatively: in general how are multiple dialogue boxes built, as parent-child relation?

libinsert will not be a child of MainWindow. QDialog-based windows are always top-level windows, regardless of what you pass as their parent when you construct them.
If there will only ever exist a single instance (per process) of MainWindow at any given time, you can give MainWindow a static function which returns a pointer to the window:
class MainWindow : public QMainWindow
{
public:
MainWindow()
{
theWindow = this;
}
virtual ~MainWindow()
{
theWindow = nullptr;
}
static get()
{
return theWindow;
}
static MainWindow *theWindow;
}
MainWindow *MainWindow::theWindow = nullptr;
Your dialogs can then access the main window's members thusly:
MainWindow::get()->someMemberFunction();
A more robust approach is to have libinsert store a pointer to the main window when it is created:
class MainWindow;
class libinsert : public QDialog
{
public:
libinsert(MainWindow *mw)
: mainWindow(mw)
{ ... }
MainWindow *getMainWindow()
{
return mainWindow;
}
MainWindow *mainWindow;
}
When MainWindow creates the dialog, it passes a pointer to itself to the constructor:
void MainWindow::createDialog()
{
libinsert *dialog = new libinsert(this):
}
This approach will work even if there are multiple instances of MainWindow active at the same time.

Related

Close QWebEngineProcess.exe

I have a problem that, after closing a QDialog with web content, the QWebEngineProcess.exe process is not closing.
Here is a minimal example:
#include <QApplication>
#include <QWebEngineView>
#include <QWebEngineSettings>
#include <QDialog>
#include <QMainWindow>
#include <QLayout>
#include <QtGui>
#include <QPushButton>
class Dialog : public QDialog
{
public:
Dialog() : QDialog(nullptr)
{
resize(512, 512);
setAttribute(Qt::WA_DeleteOnClose);
auto verticalLayout = new QVBoxLayout(this);
verticalLayout->setSpacing(0);
verticalLayout->setContentsMargins(0, 0, 0, 0);
m_webView = new QWebEngineView(this);
verticalLayout->addWidget(m_webView);
}
void openPage(const QUrl& url)
{
m_webView->setUrl(url);
}
private:
QWebEngineView* m_webView;
};
class MainWindow : public QMainWindow
{
public:
MainWindow() : QMainWindow(nullptr)
{
resize(512, 512);
QPushButton* btn = new QPushButton("open web dialog", this);
connect(btn, &QPushButton::clicked, [this] ()
{
if (m_dialog == nullptr)
{
m_dialog = new Dialog();
m_dialog->openPage(QUrl("https://www.qt.io"));
m_dialog->show();
}
});
}
private:
QPointer<Dialog> m_dialog;
};
int main(int argc, char *argv[])
{
QCoreApplication::setOrganizationName("QtExamples");
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
I'm expecting that, after the dialog is closed, QWebEngineProcess.exe will be closed too, because I'm not using webengine anymore.
P.S. During opening WebPage, I have 2 QWebEngineProcess.exe. One is disappearing, but the second one left.
For any QObject instances (such as QDialog and QWebEngineView), the destructor of a parent object destroys all child objects. This means, for every object you new, it must either have a parent, or you must delete it manually, otherwise you are creating a leak and objects will remain open (not deleted).
You are creating a QWebEngineView object with the correct parent, but the parent (the Dialog) doesn't have a parent (you initialize the parent to nullptr). This means the Dialog would delete the QWebEngineView, except it never is deleted itself.
To fix it, either delete the Dialog (stored in m_dialog) manually in the MainWindow destructor (not recommended), or initialize it correctly with the MainWindow as its parent.
This may also be related to a bug in Qt 5.11 and 5.12. If this is the problem, the recommended solution is to add QCoreApplication::setAttribute(Qt::AA_UseOpenGLES); at the top of your main() function (before declaring the QApplication).

How can I connect two windows in Qt?

I am trying make window with chat, and "main" window. If I click at username in chat window, it should show profile in main window. What's the best way to do something like this?
You should pass pointer to one window class from another and connect them by slots/signals:
class MainWindow
{
Q_OBJECT
...
public slots:
void onUsernameSelected(...);
};
class ChatWindow
{
Q_OBJECT
...
MainWindow *mainWindow;
...
ChatWindow(QObject *parent, MainWindow *mainWindow):
...
mainWindow(mainWindow)
{
connect(this, &ChatWindow::usernameSelected, mainWindow, &MainWindow::onUsernameSelected);
}
};

Qt incorrectly lays widgets on top of each other

In Qt 4.8.5 32-bit and VS2010, I'm trying to create a Window as shown in this screenshot from QtDesigner:
When I run the application, the widgets get laid down on top of each other :
In the Console, I see this :
Does anybody have any idea why this is happening?
Here is my code :
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(){
ui = new Ui::AView();
ui->setupUi(this);
}
~MainWindow();
...
private:
Ui::AView* ui;
}
From the screenshot of QtDesigner, I see that your UI is for a QWidget, but you are applying it to a QMainWindow. This causes the problem, as the handling of main window is different to other widgets.
I don't know of a way to change the base class for a UI file. Maybe the best way is to create a new file, select the mainwindow template, and copy/paste the content from the old file.
Another option would be to have a QWidget, set it up with the UI, and add it as the main window's central widget:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(){
ui = new Ui::AView();
QWidget *wgt = new QWidget;
ui->setupUi(wgt);
this->setCentralWidget(wgt);
}
~MainWindow();
...
private:
Ui::AView* ui;
}
Try this:
class MainWindow : public QMainWindow, public Ui::AView
{
Q_OBJECT
public:
MainWindow(){
setupUi(this);
}
~MainWindow();
}

How to call up a window only once with Qt

I want to know if there is any other way to call up a window once and not to call the desired window over again. This is the code I used :
void secondWindow::on_pushButton_clicked()
{
MainWindow *mainwindow;
mainwindow = new MainWindow(this);
mainwindow->show();
}
When you click the pushButton, it keeps on reproducing a new window. So is there a way it can show the same MainWindow whenever the pushButton is clicked?
I would solve this problem in one of the following ways:
1) Disable the pushButton button once it is clicked, and the MainWindow is created,
2) Declare the pointer to MainWindow as a secondWindow class' member variable:
class secondWindow : public ...
{
public:
secondWindow()
{
m_mainWindow = new MainWindow(this);
}
private slots:
void secondWindow::on_pushButton_clicked()
{
m_mainwindow->show();
}
private:
MainWindow *m_mainWindow;
};
Following will do what you want
void secondWindow::on_pushButton_clicked()
{
static MainWindow* mainwindow = new MainWindow(this);
mainwindow->show();
mainwindow->raise();
}
You should only initialize a new object once. Otherwise you only have to display the Window object.
class MainWindowFactory {
private:
MainWindow* mainWindow;
public:
MainWindowFactory();
MainWindow* getMainWindow();
};
MainWindowFactory::MainWindowFactory() {
mainWindow = NULL;
}
MainWindow* MainWindowFactory::getMainWindow() {
if (!mainWindow) {
mainWindow = new MainWindow(this);
}
mainWindow->show();
}

How to close main window from another class?

I have a class which inherits from QDialog (dialog_game_over). I show an object of that class in a slot of another class. I want to close mainwindow and QDialog when user clicked on ok button. First I built an object of mainwindow in dialog_game_over and close it. But this way it wasn't correct. What do I do for closing program in class other than main class?
dilog_game_over::dilog_game_over(QWidget *parent) :
QDialog(parent),x_size(400),y_size(400)
{
ok=new QPushButton(this);
ok->setText("OK");
ok->move(200,200);
connect(ok,SIGNAL(clicked()),this,SLOT(on_ok_clicked()));
}
void dilog_game_over::on_ok_clicked()
{
accept();
this->close();
}
class Myenemy1 : public QGraphicsObject
{
Q_OBJECT
public slots:
void loss();
private:
dilog_game_over dlg;
}
void Myenemy1::loss()
{
....
dlg.exec();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),size_of_plane_y(600),size_of_plane_x(2500)
{
set_scene();
put_cloud();
put_point();
}
All over your application you can exit by :
qApp->quit();
Or
qApp->exit(0);