1 标准对话框

image.png

  1. // 消息框
  2. MainWindow::MainWindow(QWidget *parent)
  3. : QMainWindow(parent)
  4. , ui(new Ui::MainWindow)
  5. {
  6. ui->setupUi(this);
  7. connect(ui->actionNew,&QAction::triggered,this,[=]()
  8. {
  9. //QMessageBox::critical(this,"错误","critical");
  10. //QMessageBox::information(this,"信息","info");
  11. // if(QMessageBox::question(this,"问题","question",QMessageBox::Save|QMessageBox::Cancel,
  12. // QMessageBox::Cancel) == QMessageBox::Save)
  13. // {
  14. // qDebug() << "点击的是保存";
  15. // }
  16. // else
  17. // {
  18. // qDebug() << "点击的是取消";
  19. // }
  20. QMessageBox::warning(this,"警告","warning");
  21. });
  22. }
  23. // 颜色对话框 与 文件对话框
  24. MainWindow::MainWindow(QWidget *parent)
  25. : QMainWindow(parent)
  26. , ui(new Ui::MainWindow)
  27. {
  28. ui->setupUi(this);
  29. connect(ui->actionNew,&QAction::triggered,this,[=]()
  30. {
  31. // 选择颜色对话框
  32. //QColor cr = QColorDialog::getColor(QColor(255,0,0));
  33. //qDebug() << cr.red() << cr.green() << cr.blue(); // 170 85 255
  34. // 选择文件对话框
  35. QString path = QFileDialog::getOpenFileName(this,"请选择你打开的文件","C:/Users/16481/Desktop","(*.txt *.jpg)");
  36. qDebug() << path; // "C:/Users/16481/Desktop/新建位图图像.jpg"
  37. });
  38. }

2 自定义对话框

  1. MainWindow::MainWindow(QWidget *parent)
  2. : QMainWindow(parent)
  3. , ui(new Ui::MainWindow)
  4. {
  5. ui->setupUi(this);
  6. //点击新建菜单项弹出对话框
  7. connect(ui->actionNew,&QAction::triggered,this,[=]()
  8. {
  9. // QDialog dlg(this);
  10. // dlg.resize(200,100);
  11. // dlg.exec(); //以模态显示对话框
  12. //QDialog dlg2(this); //非模态显示对话框,要创建到堆上
  13. QDialog* dlg2 = new QDialog(this);
  14. dlg2->resize(200,100);
  15. dlg2->show();
  16. dlg2->setAttribute(Qt::WA_DeleteOnClose); //设置窗口属性:关闭窗口后销毁对象
  17. });
  18. }

3 自定义消息框

  1. from PySide2.QtWidgets import QMessageBox, QApplication, QWidget, QPushButton, QAbstractButton
  2. from PySide2.QtCore import QTimer
  3. import sys
  4. class TimeMsgBox(QMessageBox):
  5. def __init__(self, title: str, text: str, timeout=5):
  6. super().__init__()
  7. self.timeout = timeout
  8. # self.setIcon(QMessageBox.Warning) # 设置图标后会有提示音
  9. self.setWindowTitle(title)
  10. self.setText(text)
  11. self.btn_accept = QPushButton(f"确 定 ({self.timeout})")
  12. self.btn_reject = QPushButton("取 消")
  13. self.addButton(self.btn_accept, QMessageBox.ButtonRole.AcceptRole)
  14. self.addButton(self.btn_reject, QMessageBox.ButtonRole.RejectRole)
  15. self.timer = QTimer(self)
  16. self.timer.timeout.connect(self.on_timer_timeout)
  17. self.timer.start(1000)
  18. def on_timer_timeout(self):
  19. self.timeout -= 1
  20. self.btn_accept.setText(f"确 定 ({self.timeout})")
  21. if self.timeout <= 0:
  22. self.btn_accept.click()
  23. class Example(QWidget):
  24. def __init__(self):
  25. super().__init__()
  26. btn = QPushButton("Button", self)
  27. btn.resize(btn.sizeHint())
  28. btn.move(50, 50)
  29. self.setWindowTitle("Example")
  30. btn.clicked.connect(self.warning)
  31. def warning(self):
  32. msgBox = TimeMsgBox("天星科技", "定时关机时间到, 是否关机?")
  33. ret = msgBox.exec_()
  34. if msgBox.clickedButton() == msgBox.btn_accept:
  35. print("1")
  36. else:
  37. print("2")
  38. if __name__ == '__main__':
  39. app = QApplication(sys.argv)
  40. ex = Example()
  41. ex.show()
  42. sys.exit(app.exec_())