Results 1 to 16 of 16

Thread: IS this allright + how to see if it exist

  1. #1
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default IS this allright + how to see if it exist

    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
    Qt Code:
    1. #include "DMain.h"
    2. #include "DBlock.h"
    3. #include "ui_Dmain.h"
    4. #include "ui_DBlock.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::on_pushButton_clicked()
    19. {
    20. BlockDialog* BlockDialogv = new BlockDialog(this);
    21. if(!BlockDialogv->isVisible()) // this is not working
    22. {
    23. BlockDialogv->show();
    24. BlockDialogv->activateWindow();
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    DBlock.cpp
    Qt Code:
    1. #include "DBlock.h"
    2. #include "ui_DBlock.h"
    3.  
    4. BlockDialog::BlockDialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::BlockDialog)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. BlockDialog::~BlockDialog()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: IS this allright + how to see if it exist

    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:n_pushButton_clicked()

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


    thats how it worked for me ...

  3. #3
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: IS this allright + how to see if it exist

    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.

  4. #4
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: IS this allright + how to see if it exist

    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..

  5. #5
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: IS this allright + how to see if it exist

    Quote Originally Posted by chetu1984 View Post
    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.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: IS this allright + how to see if it exist

    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:
    Qt Code:
    1. class BlockDialog;
    2.  
    3. class MainWindow : public QMainWindow ...
    4. ...
    5. protected:
    6. BlockDialog * _dialog;
    7. ...
    To copy to clipboard, switch view to plain text mode 
    next thing to do is to change your implementation files:
    Qt Code:
    1. // main.cpp
    2. #include "DMain.h"
    3. #include "DBlock.h"
    4. #include "BlockDialog.h" // included here, not in header
    5. #include "ui_Dmain.h"
    6. #include "ui_DBlock.h"
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow),
    11. _dialog(NULL)
    12. {
    13. ui->setupUi(this);
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. delete _dialog;
    20. }
    21.  
    22. void MainWindow::on_pushButton_clicked()
    23. {
    24. if( _dialog == NULL ){
    25. _dialog = new BlockDialog(this);
    26. _dialog->show();
    27. _dialog->activateWindow();
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    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.

  7. #7
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: IS this allright + how to see if it exist

    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?

  8. #8
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: IS this allright + how to see if it exist

    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...
    Last edited by pan; 18th March 2011 at 12:25. Reason: reformatted to look better

  9. #9
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: IS this allright + how to see if it exist

    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?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: IS this allright + how to see if it exist

    Quote Originally Posted by Nazgul View Post
    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=...esigner_2forms
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: IS this allright + how to see if it exist

    Quote Originally Posted by wysota View Post
    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=...esigner_2forms
    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
    Qt Code:
    1. void BlockDialog::closeEvent(QCloseEvent* event)
    2. {
    3. this->setVisible(false);
    4. //event->accept(); // does not even compile
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Nazgul; 18th March 2011 at 16:44.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: IS this allright + how to see if it exist

    Quote Originally Posted by Nazgul View Post
    but for the close event, it seems to not work
    Qt Code:
    1. void BlockDialog::closeEvent(QCloseEvent* event)
    2. {
    3. this->setVisible(false);
    4. //event->accept(); // does not even compile
    5. }
    To copy to clipboard, switch view to plain text mode 
    What is this meant to do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: IS this allright + how to see if it exist

    Quote Originally Posted by wysota View Post
    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.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: IS this allright + how to see if it exist

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    Nazgul (19th March 2011)

  16. #15
    Join Date
    Mar 2011
    Posts
    33
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: IS this allright + how to see if it exist

    thanks, this works 100%, did not know it was that easy. +

    if interested to more new people

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. static BlockDialog *_DDialog = new BlockDialog(this);
    4.  
    5. if(!_DDialog->isVisible())
    6. {
    7. _DDialog->show();
    8. _DDialog->activateWindow();
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: IS this allright + how to see if it exist

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. cannot find lfreetype. but it exist. what the !???
    By vuliad in forum Installation and Deployment
    Replies: 1
    Last Post: 13th November 2010, 15:08
  2. rcc: File does not exist
    By lyuts in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2010, 09:26
  3. Replies: 1
    Last Post: 2nd October 2010, 23:11
  4. How to check Table already exist or not
    By damodharan in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2010, 14:06
  5. XML Database: eXist
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2007, 00:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.