Qt Creator 显示时间 退出界面 产生新的界面 上传本地文件界面

客官°小女子只卖身不卖艺 2023-06-08 14:54 153阅读 0赞
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QTimer>
  5. #include <QDateTime>
  6. namespace Ui {
  7. class MainWindow;
  8. }
  9. class MainWindow : public QMainWindow
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit MainWindow(QWidget *parent = 0);
  14. ~MainWindow();
  15. private:
  16. Ui::MainWindow *ui;
  17. QTimer qtimer;
  18. private slots:
  19. void ExeClose();
  20. void TimerProc();
  21. void ReLogin();
  22. };
  23. #endif // MAINWINDOW_H

mainwindow.h

  1. #include "mainwindow.h"
  2. #include "login.h"
  3. #include "ui_mainwindow.h"
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(ExeClose()));
  10. connect(ui->actionRelogin, SIGNAL(triggered()), this, SLOT(ReLogin()));
  11. connect(&qtimer,SIGNAL(timeout()),SLOT(TimerProc()));
  12. qtimer.start(1000);
  13. }
  14. MainWindow::~MainWindow()
  15. {
  16. delete ui;
  17. }
  18. void MainWindow::ExeClose()
  19. {
  20. ui->statusBar->showMessage("Close MainWindow", 3000);
  21. close();
  22. }
  23. void MainWindow::TimerProc()
  24. {
  25. ui->statusBar->showMessage(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss zzz"));
  26. }
  27. void MainWindow::ReLogin()
  28. {
  29. Login * LoginFrm = new Login(this);
  30. LoginFrm->setModal(true);
  31. LoginFrm->showNormal();
  32. }

mainwindow.cpp

设置新界面

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzc3ODA0_size_16_color_FFFFFF_t_70

  1. #ifndef LOGIN_H
  2. #define LOGIN_H
  3. #include <QDialog>
  4. #include <QLineEdit>
  5. #include <QGridLayout>
  6. #include <QPushButton>
  7. #include <QFileDialog>
  8. namespace Ui {
  9. class Login;
  10. }
  11. class Login : public QDialog
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit Login(QWidget *parent = 0);
  16. ~Login();
  17. private:
  18. Ui::Login * ui;
  19. QLineEdit * p_lineEdit;
  20. QGridLayout * p_MainLayOut;
  21. QPushButton * p_QPushButton;
  22. private slots:
  23. void ShowFile();
  24. };
  25. #endif // LOGIN_H

login.h

  1. #include "login.h"
  2. #include "ui_login.h"
  3. Login::Login(QWidget *parent) :
  4. QDialog(parent),
  5. ui(new Ui::Login)
  6. {
  7. ui->setupUi(this);
  8. p_lineEdit = new QLineEdit(this);
  9. p_MainLayOut = new QGridLayout(this);
  10. p_QPushButton = new QPushButton(this);
  11. p_MainLayOut->addWidget(p_lineEdit, 0, 0);
  12. p_MainLayOut->addWidget(p_QPushButton, 0, 1);
  13. connect(p_QPushButton, SIGNAL(clicked()), this, SLOT(ShowFile()));
  14. }
  15. Login::~Login()
  16. {
  17. delete ui;
  18. }
  19. void Login::ShowFile()
  20. {
  21. QString FileName = QFileDialog::getOpenFileName(0, "打开文件", "", "C++ Files(*.cpp)");
  22. p_lineEdit->setText(FileName);
  23. }

login.cpp

ui 界面代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>898</width>
  10. <height>563</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralWidget"/>
  17. <widget class="QMenuBar" name="menuBar">
  18. <property name="geometry">
  19. <rect>
  20. <x>0</x>
  21. <y>0</y>
  22. <width>898</width>
  23. <height>23</height>
  24. </rect>
  25. </property>
  26. <widget class="QMenu" name="menuFile">
  27. <property name="title">
  28. <string>File</string>
  29. </property>
  30. <addaction name="actionSave"/>
  31. <addaction name="actionSave_2"/>
  32. <addaction name="separator"/>
  33. <addaction name="actionRelogin"/>
  34. </widget>
  35. <widget class="QMenu" name="menuClose">
  36. <property name="title">
  37. <string>Close</string>
  38. </property>
  39. <addaction name="actionClose"/>
  40. </widget>
  41. <addaction name="menuFile"/>
  42. <addaction name="menuClose"/>
  43. </widget>
  44. <widget class="QToolBar" name="mainToolBar">
  45. <attribute name="toolBarArea">
  46. <enum>TopToolBarArea</enum>
  47. </attribute>
  48. <attribute name="toolBarBreak">
  49. <bool>false</bool>
  50. </attribute>
  51. </widget>
  52. <widget class="QStatusBar" name="statusBar"/>
  53. <action name="actionSave">
  54. <property name="text">
  55. <string>Open</string>
  56. </property>
  57. </action>
  58. <action name="actionSave_2">
  59. <property name="text">
  60. <string>Save</string>
  61. </property>
  62. </action>
  63. <action name="actionRelogin">
  64. <property name="text">
  65. <string>ReLogin</string>
  66. </property>
  67. </action>
  68. <action name="actionClose">
  69. <property name="text">
  70. <string>Close</string>
  71. </property>
  72. </action>
  73. </widget>
  74. <layoutdefault spacing="6" margin="11"/>
  75. <resources/>
  76. <connections/>
  77. </ui>

mainwindow.ui

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Login</class>
  4. <widget class="QDialog" name="Login">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>400</width>
  10. <height>300</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>Dialog</string>
  15. </property>
  16. <widget class="QDialogButtonBox" name="buttonBox">
  17. <property name="geometry">
  18. <rect>
  19. <x>30</x>
  20. <y>240</y>
  21. <width>341</width>
  22. <height>32</height>
  23. </rect>
  24. </property>
  25. <property name="orientation">
  26. <enum>Qt::Horizontal</enum>
  27. </property>
  28. <property name="standardButtons">
  29. <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
  30. </property>
  31. </widget>
  32. </widget>
  33. <resources/>
  34. <connections>
  35. <connection>
  36. <sender>buttonBox</sender>
  37. <signal>accepted()</signal>
  38. <receiver>Login</receiver>
  39. <slot>accept()</slot>
  40. <hints>
  41. <hint type="sourcelabel">
  42. <x>248</x>
  43. <y>254</y>
  44. </hint>
  45. <hint type="destinationlabel">
  46. <x>157</x>
  47. <y>274</y>
  48. </hint>
  49. </hints>
  50. </connection>
  51. <connection>
  52. <sender>buttonBox</sender>
  53. <signal>rejected()</signal>
  54. <receiver>Login</receiver>
  55. <slot>reject()</slot>
  56. <hints>
  57. <hint type="sourcelabel">
  58. <x>316</x>
  59. <y>260</y>
  60. </hint>
  61. <hint type="destinationlabel">
  62. <x>286</x>
  63. <y>274</y>
  64. </hint>
  65. </hints>
  66. </connection>
  67. </connections>
  68. </ui>

login.ui

最后主函数 main.cpp

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include "login.h"
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. MainWindow w;
  8. ///*
  9. Login * LoginFrm = new Login();
  10. LoginFrm->setModal(true);
  11. LoginFrm->show();
  12. //*/
  13. w.show();
  14. return a.exec();
  15. }

20191025172624526.png

运行结果如下

1.显示时间

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzc3ODA0_size_16_color_FFFFFF_t_70 1

2.退出界面

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzc3ODA0_size_16_color_FFFFFF_t_70 2

3.产生新窗口 和 上传本地文件

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzc3ODA0_size_16_color_FFFFFF_t_70 3

自己还是太菜 害

发表评论

表情:
评论列表 (有 0 条评论,153人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Qt:用户界面

    Qt框架的主要用户界面技术是Qt快速控制和Qt小部件。Qt快速控制界面是流动的,动态的,最好的触摸界面。Qt小部件用于创建复杂的桌面应用程序。您可以创建带有目标平台原生外观的Q

    相关 Qt漂亮界面

    > 要做一个类似下面的qt界面,顶部是导航栏的样子——包含图标和程序名称,右边是界面切换按钮,然后放大、缩小和关闭按钮也在,还能拖动。这样,就更像一个商用的软件了。 ![在这