Results 1 to 13 of 13

Thread: How do i close a buttonless dialog.

  1. #1
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How do i close a buttonless dialog.

    Good day.

    I am opening a dialog that contains no buttons while the OS performs the task I have requested it to do.

    I am using a QMessageBox to confirm the action is to be performed with an OK and Cancel button.

    If OK is clicked a new form is opened up to serve as visual notification that the requested action is being completed i.e Hey, I'm doing this... be patient.

    I would like for this second form to be closed once the OS has finished its task.

    I can not figure out out to simply close the second form.

    I am opening the second form with:

    Qt Code:
    1. void MainWindow::on_actionPurge_Cache_triggered()
    2. {
    3. QMessageBox purgeMsgBox;
    4. purgeMsgBox.setWindowTitle("Purge Cache Request");
    5. purgeMsgBox.setText("Are you sure you would like to purge the local cache?");
    6. purgeMsgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    7. purgeMsgBox.setDefaultButton(QMessageBox::Cancel);
    8. int purgeReply = purgeMsgBox.exec();
    9. switch (purgeReply)
    10. {
    11. case QMessageBox::Cancel:
    12. qDebug() << "We Clicked cancel";
    13. break;
    14. case QMessageBox::Ok:
    15. qDebug() << "We clicked ok";
    16. purge dialog(this);
    17. if (dialog.exec() != QDialog::Accepted)
    18. return;
    19. break;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    Am I opening the second form "purge" incorrectly?
    The second form simply removes some local files, and should close once completed. Yet I can not figure out how to close the form.

    Once the second form is instantiated I am calling the following function to remove some local files:
    Qt Code:
    1. void purge::purgeCache()
    2. {
    3. qDebug() << "We are in function to purge cache";
    4. QString cacheLocation = "/var/cache/lxc";
    5. QString removeProgram = "/bin/rm";
    6. QStringList arguements;
    7. arguements << "-rf" << cacheLocation;
    8. QProcess *removeProcess = new QProcess(this);
    9. removeProcess->start(removeProgram, arguements);
    10. removeProcess->waitForFinished(-1);
    11. }
    To copy to clipboard, switch view to plain text mode 

    Do I create a signal to indicate the purge action is complete, and connect it to the close slot?

    Your help is appreciated

  2. #2
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do i close a buttonless dialog.

    You can just call the "accept()" slot after purgeCache is finished.
    But as a reminder, calling it from the constructor could possibly leads to a failure coz the dialog is not created yet at that time.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  3. #3
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i close a buttonless dialog.

    As you suggested, I called the accept() slot after the purgeCache function is completed.

    However, the form does not close.

  4. #4
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do i close a buttonless dialog.

    Need more code to see...
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  5. #5
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i close a buttonless dialog.

    Thank you for your assistance.
    This is my first c++ program so please be patient with me

    Please forgive the long post. The following is a small app that I am using for testing before I add it into the real app.

    First up is mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "purge.h"
    4. #include <QtGui>
    5. #include <QProcess>
    6. #include <QTimer>
    7. #include <QtDebug> // don't forget to remove when we are done
    8. //#include <QDialog>
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14. ui->setupUi(this);
    15. QTimer *timer = new QTimer(this);
    16. connect( timer, SIGNAL(timeout()), SLOT(listContainers()) );
    17. timer->start(5000);
    18. listContainers(); // implicitly call the function to display the containers
    19. // instead of waiting for the timer
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26.  
    27. void MainWindow::changeEvent(QEvent *e)
    28. {
    29. QMainWindow::changeEvent(e);
    30. switch (e->type()) {
    31. case QEvent::LanguageChange:
    32. ui->retranslateUi(this);
    33. break;
    34. default:
    35. break;
    36. }
    37. }
    38.  
    39. // List containers registered with LXC. We use stdout from lxc-ls to grab the container name
    40. // and then we use stdout from lxc-info to determine the status of the container.
    41. void MainWindow::listContainers()
    42. {
    43. ui->treeWidget->header()->setResizeMode(0, QHeaderView::Stretch);
    44. ui->treeWidget->header()->setResizeMode(1, QHeaderView::Stretch);
    45. ui->treeWidget->clear();
    46. QString listProgram = "/usr/bin/lxc-ls";
    47. QProcess *listProcess = new QProcess( this );
    48. listProcess->start(listProgram);
    49. listProcess->waitForFinished(-1);
    50. QString listOutput;
    51. do
    52. {
    53. item = new QTreeWidgetItem;
    54. listOutput = listProcess->readLine();
    55. listOutput.chop(1);
    56. if (listOutput.isEmpty())
    57. break;
    58. QString infoProgram = "/usr/bin/lxc-info";
    59. QStringList arguements;
    60. arguements << "-n" << listOutput;
    61. QProcess *infoProcess = new QProcess( this );
    62. infoProcess->start(infoProgram, arguements);
    63. infoProcess->waitForFinished(-1);
    64. QString infoOutput = infoProcess->readLine();
    65. // The output of lxc-info includes the container name as well as the status,
    66. // but the status is in all caps.
    67. // Grab just the status and then beautify it.
    68. infoOutput.chop(1);
    69. if (infoOutput.contains("RUNNING", Qt::CaseInsensitive))
    70. infoOutput = "Running";
    71. if (infoOutput.contains("STOPPED", Qt::CaseInsensitive))
    72. infoOutput = "Shutoff";
    73. if (infoOutput.contains("FROZEN", Qt::CaseInsensitive))
    74. infoOutput = "Paused";
    75. item->setText(0, listOutput);
    76. item->setText(1, infoOutput);
    77. ui->treeWidget->addTopLevelItem(item);
    78. }
    79. while (listOutput != NULL);
    80. };
    81.  
    82. // Use a messagebox to confirm we want the cache removed and then
    83. // display a new form until the cache removal process is complete.
    84. void MainWindow::on_actionPurge_Cache_triggered()
    85. {
    86. qDebug() << "Displaying confirmation message";
    87. QMessageBox purgeMsgBox;
    88. purgeMsgBox.setWindowTitle("Purge Cache Request");
    89. purgeMsgBox.setText("Are you sure you would like to purge the local cache?");
    90. purgeMsgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    91. purgeMsgBox.setDefaultButton(QMessageBox::Cancel);
    92. int purgeReply = purgeMsgBox.exec();
    93. switch (purgeReply)
    94. {
    95. case QMessageBox::Cancel:
    96. qDebug() << "We Clicked cancel";
    97. break;
    98. case QMessageBox::Ok:
    99. qDebug() << "We clicked ok";
    100. purge dialog(this);
    101. dialog.exec();
    102. if (dialog.exec() != QDialog::Accepted)
    103. return;
    104. break;
    105. }
    106. }
    To copy to clipboard, switch view to plain text mode 

    Next up is mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTreeWidget>
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow {
    12. Q_OBJECT
    13. public:
    14. MainWindow(QWidget *parent = 0);
    15. ~MainWindow();
    16.  
    17. protected:
    18. void changeEvent(QEvent *e);
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22.  
    23. private slots:
    24. void on_actionPurge_Cache_triggered();
    25. void listContainers();
    26.  
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Next we have the window that I can't seem to close.
    purge.cpp
    Qt Code:
    1. #include "purge.h"
    2. #include "ui_purge.h"
    3. #include <QtDebug>
    4. #include <QProcess>
    5.  
    6. purge::purge(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::purge)
    9. {
    10. ui->setupUi(this);
    11. purgeCache();
    12. qDebug() << "we are no longer in the purgeCache function";
    13.  
    14. }
    15.  
    16. purge::~purge()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void purge::changeEvent(QEvent *e)
    22. {
    23. QDialog::changeEvent(e);
    24. switch (e->type()) {
    25. case QEvent::LanguageChange:
    26. ui->retranslateUi(this);
    27. break;
    28. default:
    29. break;
    30. }
    31. }
    32.  
    33. // Use QProcess to monitor the removal of the local cache and wait until the cache is removed
    34. // until we exit the function
    35. void purge::purgeCache()
    36. {
    37. qDebug() << "We are in function to purge cache";
    38. QString cacheLocation = "/var/cache/lxc";
    39. QString removeProgram = "/bin/rm";
    40. QStringList arguements;
    41. arguements << "-rf" << cacheLocation;
    42. QProcess *removeProcess = new QProcess(this);
    43. removeProcess->start(removeProgram, arguements);
    44. removeProcess->waitForFinished(-1);
    45. qDebug() << "purge process has completed";
    46. close();
    47. }
    To copy to clipboard, switch view to plain text mode 

    And the header for purge:
    Qt Code:
    1. #ifndef PURGE_H
    2. #define PURGE_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class purge;
    8. }
    9.  
    10. class purge : public QDialog {
    11. Q_OBJECT
    12. public:
    13. purge(QWidget *parent = 0);
    14. ~purge();
    15.  
    16. protected:
    17. void changeEvent(QEvent *e);
    18.  
    19. private:
    20. Ui::purge *ui;
    21. void purgeCache();
    22.  
    23. };
    24.  
    25. #endif // PURGE_H
    To copy to clipboard, switch view to plain text mode 

    And lastly, main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do i close a buttonless dialog.

    Well, I think I've told you not to call the accept slot in the constructor in my last reply...
    try calling the purgeCache function in http://doc.qt.io/qt-4.8/qwidget.html#showEvent
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  7. #7
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i close a buttonless dialog.

    Updated mainwindow.cpp to show the form with:
    Qt Code:
    1. purge dialog(this);
    2. dialog.exec()
    To copy to clipboard, switch view to plain text mode 

    Updated purge.h to:
    Qt Code:
    1. protected:
    2. void changeEvent(QEvent *e);
    3. void showEvent(QShowEvent *);
    4. void closeEvent(QCloseEvent *e);
    To copy to clipboard, switch view to plain text mode 

    Updated purge.cpp to make a call to purgeCache function in the showEvent:
    Qt Code:
    1. void purge::showEvent(QShowEvent *)
    2. {
    3. qDebug() << "in the show event";
    4. purge::purgeCache();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Updated purge.cpp to output when the closeEvent occurs:
    Qt Code:
    1. void purge::closeEvent(QCloseEvent *e)
    2. {
    3. qDebug() << " in the close event";
    4. }
    To copy to clipboard, switch view to plain text mode 

    I can clearly see the call to close the window, yet it does not close.
    Also, now that I am calling the purgeCache function from the showEvent, and the purgeCache function includes "close()" the form will not close whatsoever. Every time I click the close button on the window manager, it outputs "in the close event", so I show to be in the close handler, yet it doesn't happen, and I have to kill the app.

    Once again, I apprecaite your input

  8. #8
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i close a buttonless dialog.

    I had to cheat to make it work the way I wanted.

    I added a check in the changeEvent:
    Qt Code:
    1. if (strPurged == "True")
    2. close();
    To copy to clipboard, switch view to plain text mode 

    At the end of the purgeCache function I assign True to strPurged.

    I noticed that even though I enter the closeEvent and the window wouldn't close, that a call was made to the changeEvent since closing is a changeEvent.

    Although this is a bit hackish it works. If there is a better way please let me know.

  9. #9
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do i close a buttonless dialog.

    After you override those virtual function, you should call the original version to make it do original things. e.g.
    Qt Code:
    1. void purge::closeEvent(QCloseEvent *e)
    2. {
    3. qDebug() << " in the close event";
    4. QDialog::closeEvent(e);
    5. }
    To copy to clipboard, switch view to plain text mode 
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  10. #10
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i close a buttonless dialog.

    Will try thanks for the help.

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How do i close a buttonless dialog.

    Create a timer with some timeout constants and then connect its timeout() signal to window close slot.
    Qt 5.3 Opensource & Creator 3.1.2

  12. #12
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do i close a buttonless dialog.

    Thanks for the reply MarkoSan.

    I thought about the timer idea, but since I will be removing local files, it would be difficult to determine how long to wait for the timeout. I could run some test with a stopwatch, but the cache size on disk will change depending on what is cached.

    I do however, greatly appreciate the idea

    For the moment, I have it working even if it is a bit on the hackish side.

  13. #13
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How do i close a buttonless dialog.

    Hmm, then create a list of files to be deleted, from every entry create a qfile object and then call QFile::remove() method. This method returns bool (true if delete was sucessfull, false if faled, I think). The described result is added to another QList (resultList) and if every entry in result is true, close window, otherwise ...
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Delete/Close Dialog correct?
    By kei in forum Qt Programming
    Replies: 8
    Last Post: 2nd July 2009, 13:51
  2. Close Dialog in showEvent
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 11th April 2008, 15:32
  3. Block close dialog
    By Mrdata in forum Newbie
    Replies: 2
    Last Post: 12th March 2007, 15:39
  4. Replies: 2
    Last Post: 5th February 2007, 17:42
  5. Replies: 3
    Last Post: 23rd July 2006, 18:02

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
  •  
Qt is a trademark of The Qt Company.