简体   繁体   中英

open new window as a dialog (Qt4)

I created a main window and new window. when a button is pressed on the main window the new window will appear. thats all right. but I need the new window to apper as a dialog. that means whan a dialog is opened the main window will not function untill the dialog is closed. and when a dialog is opened a new tsb will not apper in the taskbar. how to do this.

here are some codes what i used,

mainwindow.h

 private:
  Form *myform;

public slots:
 void myformshow();

mainwindow.cpp

mainWin::mainWin(QWidget *parent)
{
  setupUi(this);
 connect(pushButton,SIGNAL(clicked()),this,SLOT(myformshow()));
}

  void mainWin::myformshow(){
myform= new Form(); //make sure to delete newform someware.
myform->show();
}

form.h

 class Form : public QWidget, private Ui::Form
{
 Q_OBJECT

public:
 Form(QWidget *parent);

public slots:
void  command();
};

#endif // FORM_H

here Form means, I created the new window as forms.h and its class is Form

Make the second window inherit from QDialog (not strictly necessary, but will give you correct platform-specific behavior, like centering and various window flags), set the main window as its parent, and run it using its exec() function.

First, change your Form class to inherit from QDialog:

class Form : public QDialog, private Ui::Form

(If you had references to QWidget elsewhere in your From class code, change them too.)

Then, in your mainWin::myformshow() function, do:

void mainWin::myformshow()
{
    myform = new Form; //make sure to delete newform someware.
    myform->exec();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM