How to make a window/widget keep showing?
-
Hi, I am a beginner at Qt and I'm trying to do simple things.
I´ve created a MainWindow and then created a widget.On the menu I´ve made an "about" option, that would show program version etc.For the code, I´ve put:
@
void MainWindow::on_actionAbout_triggered()
{
aboute a;
a.show();
}
@But differently from the mainwindow, the widget closes immediatly after opening.How can I make it open without closing?
thanks
[edit: fixed @ / chetankjain] -
Thank you viswesr.
@//—-mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;private slots:
void on_actionSobre_triggered();
void on_actionSair_triggered();
};#endif // MAINWINDOW_H
//———-sobre.h(“sobre” means “about” in portuguese)—-
#ifndef SOBRE_H
#define SOBRE_H
#include
namespace Ui {
class sobre;
}class sobre : public QWidget
{
Q_OBJECTpublic:
explicit sobre(QWidget *parent = 0);
~sobre();private:
Ui::sobre *ui;private slots:
void on_pushButton_clicked();
};#endif // SOBRE_H
//main.cpp————————————-
#include
#include “mainwindow.h”
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}//mainwindow.cpp—————————————————-
#include “mainwindow.h”
#include “ui_mainwindow.h”
#include “sobre.h”
#include “ui_sobre.h”
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_actionSair_triggered()
{
MainWindow::close();
}void MainWindow::on_actionSobre_triggered()
{
sobre s;
s.show();
}//-sobre.cpp——————
#include “sobre.h”
#include “ui_sobre.h”sobre::sobre(QWidget *parent) :
QWidget(parent), ui(new Ui::sobre)
{
ui->setupUi(this);
}sobre::~sobre()
{
delete ui;
}void sobre::on_pushButton_clicked()
{
sobre::close();
}
@the "sobre" widget contet is just text and a "ok" button that closes the window.
I'd like to know the general procedure to keep something wich is not the main window showing while not closed by some action.
[edit: fixed @ / chetankjain]
-
Could you please use the code tags around your code? It is hardly readable the way it is. Thanks!
-
You can do one of the following:
-
You can create the dialog on the heap (using a pointer and "new", and don't forgot to parent the dialog with "this" pointer)
-
You can create your dialog on that slot stack (like in the first post) and use a.exec() (instead of show(), it will make your dialog modal) !!!This works only if you derived your about from QDialog class
-
"QMessageBox":http://doc.qt.nokia.com/4.6/qmessagebox.html can be another good way to do this (since you don't need that many "widgets" in this)
-
-
Your code contains an error that causes your "About" window to disappear immediately after showing.
@void MainWindow::on_actionSobre_triggered()
{
sobre s;
s.show();
}@You create a sobre object on the stack, then call show(). After the on_actionSobre_triggered() finishes and the destructor of s is called. That's why your widget disappears immediately.
To get rid of this create sobre's instance dynamically with operator new.