PDA

View Full Version : IS this allright + how to see if it exist



Nazgul
17th March 2011, 17:59
Hello,

I'm new to Qt, I started actually today with it, after I was making my gui (.ui file) in the editor, I came to the problem how can I show my second window, I found a solution with google, but now im walking in next problems, so I thought let's join a Qt forum and leave my win api forum:(

Anyways, this is my code.

DMain.cpp

#include "DMain.h"
#include "DBlock.h"
#include "ui_Dmain.h"
#include "ui_DBlock.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
BlockDialog* BlockDialogv = new BlockDialog(this);
if(!BlockDialogv->isVisible()) // this is not working
{
BlockDialogv->show();
BlockDialogv->activateWindow();
}
}

DBlock.cpp

#include "DBlock.h"
#include "ui_DBlock.h"

BlockDialog::BlockDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::BlockDialog)
{
ui->setupUi(this);
}

BlockDialog::~BlockDialog()
{
delete ui;
}

I'm trying to show a second dialog from my main, as you can see I tried to check if it allready existed, but off course this did not work, quite clearly if you look at the code, but I gave it a try. I want to make it so, that it can only open once and hide itselfs after someone closes it, I've to handle that in the BlockDialog class with a slot right?

Is this further all right coded?

chetu1984
17th March 2011, 18:26
Even I am new to QT .. so what I am saying might not be totally right. but i am trying to help you.

For opening a widget form the main window. as for your application .

1. In the MainWindow.h include the header file for the widget #include "DBlock.h"

2. In MainWindow.h delcare the widget and pointer like this
private:
DBlock *dbk

3. In the Constructor for MainWindow dbk = new DBlock;

4. and in the void MainWindow::on_pushButton_clicked()

dbk.show();
dbk.raise();


thats how it worked for me ...

Nazgul
17th March 2011, 18:34
Thanks, but this works except for the fact it opens at many times you click, I want it once, if u close it, hide it, click again. show it etc,

btw, from normal C++ eyes, it's not a good habbit to decalre so many variables in a class, keep it as low as possible.

chetu1984
17th March 2011, 18:38
I am not able to understand you clearly..
you open a widget on a button click on main window and should be able to close it and open it again ..thats what you wanted to do ..correct me if i am wrong..

Nazgul
17th March 2011, 18:41
I am not able to understand you clearly..
you open a widget on a button click on main window and should be able to close it and open it again ..thats what you wanted to do ..correct me if i am wrong..

You click at the button, it opens/creates/shows the dialog, but if it exist it does nothing.

In the dialog I want when it get destroyed, is that the right message?, it becomes hidden and not destryoed.

stampede
17th March 2011, 19:50
Are you familiar with "forward declarations" in C++ ? It could be quite useful. For example, you can declare your dialog class in MainWindow header and add a member for MainWindow class:



class BlockDialog;

class MainWindow : public QMainWindow ...
...
protected:
BlockDialog * _dialog;
...


next thing to do is to change your implementation files:


// main.cpp
#include "DMain.h"
#include "DBlock.h"
#include "BlockDialog.h" // included here, not in header
#include "ui_Dmain.h"
#include "ui_DBlock.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
_dialog(NULL)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
delete _dialog;
}

void MainWindow::on_pushButton_clicked()
{
if( _dialog == NULL ){
_dialog = new BlockDialog(this);
_dialog->show();
_dialog->activateWindow();
}
}
This way the BlockDialog will be created and shown only on the first button click.
Forward declaring is not really mandatory here, but it's nice feature, worth knowing.

Nazgul
18th March 2011, 10:58
Thanks, but that does not work, it does not open once, I did exactly what you coded. But if you do it like that _dialog will not have value NULL right?

pan
18th March 2011, 11:24
You need to change the on_pushButton_clicked(): so that show() is outside the "if" statement. Otherwise once it is instantiated you won't be able to show it again...

Nazgul
18th March 2011, 14:15
Thanks, now that works Im running in weird problems

Im chaning my Dblock .ui file and compile, but when I start my apllication it does not change, if I change anything in my main it does change, weird?

wysota
18th March 2011, 14:20
btw, from normal C++ eyes, it's not a good habbit to decalre so many variables in a class, keep it as low as possible.

From normal C++ eyes if you create a new object of a class then it won't magically connect to all other instances of the class and act as the same object. If you create a new window each time you enter the method then you obviously get... many windows.

http://www.qtcentre.org/faq.php?faq=qt_general_category#faq_qt_designer_2f orms

Nazgul
18th March 2011, 14:45
From normal C++ eyes if you create a new object of a class then it won't magically connect to all other instances of the class and act as the same object. If you create a new window each time you enter the method then you obviously get... many windows.

http://www.qtcentre.org/faq.php?faq=qt_general_category#faq_qt_designer_2f orms

Yes, it was more hoping on the nothing,

but do you know my last question about the .ui file?

EDIT: fixed, rebuilded now it works again

but for the close event, it seems to not work


void BlockDialog::closeEvent(QCloseEvent* event)
{
this->setVisible(false);
//event->accept(); // does not even compile
}

wysota
18th March 2011, 19:18
but for the close event, it seems to not work


void BlockDialog::closeEvent(QCloseEvent* event)
{
this->setVisible(false);
//event->accept(); // does not even compile
}


What is this meant to do?

Nazgul
18th March 2011, 19:43
What is this meant to do?

Well, in win api you can handle the WM_CLOSE message and instead of desttroy the window and redraw it again, by pressing the button.

In the form I wanna create a listview and fill it, if I've to redraw it every time, I also have to fill it again, so hide/show is the best solution? I'm used to this in win api, i don't know if there is a better solution in Qt.

So the code should instead of deleting the dialog, hide it. So that edit boxes/listviews remain their value.

wysota
18th March 2011, 22:07
But Qt doesn't destroy widgets if you close them. You don't need such treatment. If you want to show it again then just call show(). Take a look at the link I gave you earlier, there is a complete example of reusing a dialog there.

Nazgul
18th March 2011, 22:54
thanks, this works 100%, did not know it was that easy. +

if interested to more new people


void MainWindow::on_pushButton_clicked()
{
static BlockDialog *_DDialog = new BlockDialog(this);

if(!_DDialog->isVisible())
{
_DDialog->show();
_DDialog->activateWindow();
}

}

wysota
18th March 2011, 23:01
I would remove the "if". You probably want to activate the window if the user requests it to be visible even if the window already is visible. raise() might also be useful.