QDialog-derived form closes immediately - c++

I am trying to make a form with data table appear when the button is clicked on thr main form. However, in practice second form 'blinks' - appears less than on second - and then vanished. What could be the reason and how that should be fixed?
Here is the derived form header and source files' content:
#ifndef GOODTABLE_H
#define GOODTABLE_H
#include <QDialog>
#include <QSqlTableModel>
namespace Ui {
class GoodTable;
}
class GoodTable : public QDialog
{
Q_OBJECT
public:
explicit GoodTable(QDialog *parent = 0);
GoodTable(QDialog *parent,QSqlTableModel* model);
~GoodTable();
private:
Ui::GoodTable *ui;
};
#endif // GOODTABLE_H
#include "goodtable.h"
#include "ui_goodtable.h"
GoodTable::GoodTable(QDialog *parent) :
QDialog(parent),
ui(new Ui::GoodTable)
{
ui->setupUi(this);
}
GoodTable::GoodTable(QDialog *parent,QSqlTableModel* model) :
QDialog(parent),
ui(new Ui::GoodTable)
{
ui->setupUi(this);
ui->tableView->setModel(model);
}
GoodTable::~GoodTable()
{
delete ui;
}
The code creating second window:
void MainWindow::on_goodTable_clicked()
{
QSqlTableModel model;
initializeGoodModel(&model);
//! [4]
GoodTable view(NULL,&model);
view.setWindowFlags(Qt::Window);
view.setWindowModality(Qt::ApplicationModal);
view.show();
}

The problem is, that you have a local dialog object on the stack in your on_goodTable_clicked method. So you create the view, call show, which shows the dialog and immediately returns, then your view get's destroyed as you leave the function. If you make the dialog modal anyway, why not use QDialog's exec method intead of show. It shows the dialog and blocks the main window until you clicked the dialog's Ok or cancel button and then exec finally returns. When you want a non-modal dialog (meaning your main window works, while the dialog is open), you need to create your dialog dynamically (or make it a member of your main window, or both).

Related

Calling a function that is apart of an initialized object after a member variable has changed in another file

Pretty much this window opens up and asks for a bandname. I got it so the characters on the line edit widget get stored in a variable. Problem is I have another file called main window.cpp and I want that variable to to be stored on the list widget on that window. Now I know how to display things on the list widget but I can't figure out a way to get the text after the user finished typing. The bandname var in the main window.cpp file just takes an empty string and I know why but is there any way to trigger the get call after the user has finished typing. Do I have to restrict something in the class like the get function. I've experimented a lot and saw callbacks but I could just use signals and slots. Everything Ive tried just returns an empty string but I need the text after the user has finished typing what he wants. Here is the dialog window named add button
#include "addbutton.h"
#include "ui_addbutton.h"
AddButton::AddButton(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddButton)
{
ui->setupUi(this);
connect(ui->cancel,SIGNAL(released()),this,SLOT(close()));
//Get Text when user presses enter
connect(ui->lineEdit, SIGNAL(editingFinished()),this,SLOT(setBandName()));
}
void AddButton::setBandName(){
bandname = ui->lineEdit->text();
}
void AddButton::updateState(){
pbandname = bandname;
}
QString AddButton::getBandName(){
return bandname;
}
AddButton::~AddButton()
{
delete ui;
}
Here is the main window.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addbutton.h"
#include "bandinfo.h"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mediaplayer = new Player;
connect(ui->pushbutton_addBand,SIGNAL(pressed()),this,SLOT(addBand()));
}
void MainWindow::addBand(){
BandInfo band;
AddButton *addband_window = new AddButton;
QString bandname;
addband_window->show();
bandname = addband_window->pbandname;
qDebug() << bandname;
}
MainWindow::~MainWindow()
{
delete ui;
}
I'm not sure I understand your post. Are you saying you want the main window to be notified/updated when the user finishes editing in the "AddButton" class?
If I've got that right it seems pretty straightforward:
Add a signal to the AddButton class. Call it something like "bandNameChanged".
Make the signal pass a string as its argument
Emit the signal from within AddButton::setBandName and pass the string name.
Have the main window connect a slot to the "bandNameChanged" signal when it creates the AddButton.
In the slot, update your list widget

Initializing a Ui pointer From a QMainWindow class to a QDialog Class

I'm really stuck on one problem that I want to solve. the problem is that I have a Class for QMainWindow which holds the Ui variable for that form. Now I want to be able to edit that Form using the Ui variable in that class on a QDialog cpp file. I probably sound really stupid and I really have no idea how I should explain this, but I have code which maybe can help.
MainWindow.h:
#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
Ui::MainWindow *ui;
}
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
Dialog *dialog;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
dialog = new Dialog(this);
dialog->show();
}
QDialog.cpp:
#include "ui_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;
}
Ui::MainWindow *mainui;
void Dialog::on_pushbutton_clicked(){
mainui->label->setText("test");
}
So as you can see from the above code, it shows that I have a pointer to the Ui variable however its uninitialised, therefore it would lead to a SIGSEGV error, so how to do Initialize this pointer? any help here is highly appreciated, and even though this is probably really simple I just don't know what to do. (I have looked at other questions but I couldn't quite grasp what to do, so please explain what I am to do before linking me to a similar question. Also, I have left out the Dialog.h file as I didn't think it was needed, please tell me if I need to show it, thanks!).
Generally in C++ you should practice what is called encapsulation - keep data inside a class hidden from others that don't need to know about it. It's not good to have multiple pointers to the UI object as now all those other objects have to know how the main window UI is implemented.
In this case, what I would recommend is to use Qt's signals and slots mechanism to allow the dialog to tell the main window what you need it to do. That has the advantage that if you add more dialogs, or change how things are implemented in the main window, you don't need to alter the signal slot mechanism, and the details are hidden cleanly.
So - for your dialog, add a signal like this in the header file
class Dialog : QDialog
{
Q_OBJECT
signals:
void setTextSignal(QString text);
}
and in your main window header, add a slot.
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void setTextSlot(const QString &text);
}
now in your method where the button is pressed,
void Dialog::on_pushbutton_clicked()
{
emit setTextSignal("test");
}
and in your main window
void MainWindow::setTextSlot(const QString &text)
{
mainUi->label->setText(text);
}
The final part is to connect the signal and slot together, which you would do in your main window function where you create the dialog:
void MainWindow::on_pushButton_clicked()
{
dialog = new Dialog(this);
connect(dialog, SIGNAL(setTextSignal(QString)), this, SLOT(setTextSlot(QString)));
dialog->show();
}
You can see there are many advantages to this; the Dialog no longer needs a pointer to the main window UI, and it makes your code much more flexible (you can have other objects connected to the signals and slots as well).
Short answere - your can't! If you want to create a new instance of the ui, you would have to do:
MainWindow::Ui *ui = new MainWindow::UI();
ui->setupUi(this);
However, the this-pointer for a UI created for a QMainWindow based class must inherit QMainWindow - thus, you can't.
In general, it is possible if you create your Ui based on a QWidget instead of a QMainWindow, since both inherit QWidget.
Alternativly, you could try the following:
QMainWindow *subWindow = new QMainWindow(this);
subWindow->setWindowFlags(Qt::Widget);
MainWindow::Ui *ui = new MainWindow::UI();
ui->setupUi(subWindow );
//... add the mainwindow as a widget to some layout
But I would guess the result will look weird and may not even work in the first place.

Ok button is not working on QDialog

I'm working on an app in C++/Qt I have used the design tool to design a Qdialog box.
The Dialog box is defined as below.
c++ file
#include "dialogwarning.h"
#include "ui_dialogwarning.h"
DialogWarning::DialogWarning(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogWarning)
{
ui->setupUi(this);
}
DialogWarning::~DialogWarning()
{
delete ui;
}
header file
#include <QDialog>
namespace Ui {
class DialogWarning;
}
class DialogWarning : public QDialog
{
Q_OBJECT
public:
explicit DialogWarning(QWidget *parent = 0);
~DialogWarning();
private:
Ui::DialogWarning *ui;
};
The source use it as below:
WarningDialog = new DialogWarning();
QLabel *label = new QLabel("File/Folder name already exist", WarningDialog);
label->setGeometry(WarningDialog->rect().center().x() - label->rect().width()/2,
WarningDialog->rect().center().y() - label->rect().height()*2,
WarningDialog->rect().width(),
WarningDialog->rect().height());
WarningDialog->exec();
I'm using exec instead of show because the exec primitive allow me to be stucked inside the dialog until a press on the "Ok" button.
What is strange is that the OK button is not working. I do not need any specific behavior just wait the OK press to continue to run the code.
Thanks
i'm not quite sure assuming u have connected everything. could be that the application is frozen.
why not put the calculation in a different thread and connect the same signal that shows the dialog to thread pause.
the signal of the ok pushbutten should be connected to close the dialog and thread resume...

How to easily get the button Press in a dialog box in Qt/C++

I have wrote an app which mainly is an equivalent to mac osx finder. When copying file into a folder, I'm checking if a file of the same name already exist. In case it exist, I'm asking the user if he want to cancel, overwrite or not overwrite.
The Dialog box for the overwrite has been created with QT designer and generate a dialogoverwrite.ui, .cpp and .h.
User Interface
dialogoverwrite.cpp
#include <QDialogButtonBox>
#include "dialogoverwrite.h"
#include "ui_dialogoverwrite.h"
DialogOverwrite::DialogOverwrite(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogOverwrite)
{
ui->setupUi(this);
}
DialogOverwrite::~DialogOverwrite()
{
delete ui;
}
dialogoverwrite.h
#include <QDialog>
#include <QDialogButtonBox>
namespace Ui {
class DialogOverwrite;
}
class DialogOverwrite : public QDialog
{
Q_OBJECT
public:
explicit DialogOverwrite(QWidget *parent = 0);
~DialogOverwrite();
private:
Ui::DialogOverwrite *ui;
};
I'm using this class in My TreeWidget application as shown below, I will only add the required code
DialogOverwrite *OverwriteDialog = new DialogOverwrite;
OverwriteDialog->exec();
A kind of OverwriteDialog.button.value could be perfect for me.
the exec will show the dialog and wait for a user action. How can I easily catch the return value : Cancel, Yes, YesToAll, No, NoToAll
I'm looking for an easy to get it. I would like to avoid any additional method in the dialogoverwrite class with signal/connecT. I really just need the button value to react.
Thanks a lot
Call QDialog::done(int r) with a value that represents one of the closing buttons. This value is returned by exec().
Default values are provided by QDialog::DialogCode enum.

Qt window wont close using "this->close()" from other class

I will start off by explaining my main goal. I have a main window with 7 buttons on it(amongst other things), when you hit each button, it closes out the current window and opens up a new window. All the windows will have the same 7 buttons, so you can go between each window. With all windows having the exact same 7 buttons, I wanted to set up a function that each class can call to set up each button and connect to a slot() in my mainwindow.cpp(called setupSubsystemButtons in example below). However, I can't seem to get the window to close using the standard "this->close()"...it works when I go from the main window to another window(the main window closes) but when I go from a different window to say the home window, the different window doesn't close. Suggestions would be greatly appreciated. My guess is that my understanding of "this" when it comes to calling slots in another class is wrong.
mainwindow.cpp( the parts that are relevant)
void MainWindow::ECSgeneralScreen()
{
ECSgeneralCommand *ECSgeneral = new ECSgeneralCommand;
this->close();
ECSgeneral->show();
//opens up the ECS screen
}
void MainWindow::homeScreen()
{
MainWindow *home = new MainWindow;
this->close();
home->show();
//opens up the ECS screen
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setupSubsystemButtons(QGridLayout *layout)
{
//Push Button Layout
homeScreenButton = new QPushButton("Home");
layout->addWidget(homeScreenButton, 3, 11);
connect(homeScreenButton, SIGNAL(clicked()), this, SLOT(homeScreen()));
ECSgeneralScreenButton = new QPushButton("General");
layout->addWidget(ECSgeneralScreenButton,5,11);
connect(ECSgeneralScreenButton, SIGNAL(clicked()), this, SLOT(ECSgeneralScreen()));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWidgets>
#include <QDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
QWidget *window;
void setupSubsystemButtons(QGridLayout *layout);
~MainWindow();
private slots:
public slots:
void ECSgeneralScreen();
void homeScreen();
};
#endif // MAINWINDOW_H
ecsgeneralcommandWindow
include "ecsgeneralcommand.h"
#include "mainwindow.h"
#include <QtWidgets>
#include <QtCore>
ECSgeneralCommand::ECSgeneralCommand(MainWindow *parent) : QDialog(parent)
{
QGridLayout *layout = new QGridLayout;
QWidget::setFixedHeight(600);
QWidget::setFixedWidth(650);
...
//Setup Subsystem Buttons
test.setupSubsystemButtons(layout);
setLayout(layout);
}
ecsgeneralcommandWindow header
#ifndef ECSGENERALCOMMAND_H
#define ECSGENERALCOMMAND_H
#include <QDialog>
#include <QMainWindow>
#include <QtWidgets>
#include <QObject>
#include "mainwindow.h"
class ECSgeneralCommand : public QDialog
{
Q_OBJECT
public:
explicit ECSgeneralCommand(MainWindow *parent = 0);
private:
MainWindow test;
public slots:
};
#endif // ECSGENERALCOMMAND_H
Slots are just normal functions. When Qt invokes a slot, it ends up calling the appropriate receiver's method. In other words, this equals to the value of the 3rd argument of your connect statements. You passed this there, so the receiver is MainWindow object. E.g. MainWindow::homeScreen method always tries to close MainWindow. If it is already hidden, this action takes no effect.
You should either have a slot in each window class and connect buttons to appropriate receivers, or use a pointer to the currently active window instead of this when calling close(). But your architecture is strange in the first place. Why would you need to create these buttons for each window? It is reasonable to create them once and use in all windows. Also hiding and showing windows is not necessary. You can create one main window with buttons and a QStackedWidget that will contain the content of all other windows. Maybe you can even use QTabWidget instead of these buttons.