zoukankan      html  css  js  c++  java
  • Qt——添加动作及对话框

    1. 添加动作

    教程:https://www.devbean.net/2012/08/qt-study-road-2-action/

    运行教程中的第一个程序,报错如下:

    原因:没有将main.cpp改为教程中的代码。见http://www.xuebuyuan.com/2029333.html

    然后根据教程,代码如下:

    //mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        void open();
    
        QAction *openAction;
    };
    
    #endif // MAINWINDOW_H
    //mainwindow.cpp
    #include <QAction>
    #include <QMenuBar>
    #include <QMessageBox>
    #include <QStatusBar>
    #include <QToolBar>
    
    #include "mainwindow.h"
    
    //说明MainWindow构造函数前需要调用QMainWindow的含参构造函数
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        setWindowTitle(tr("Main Window"));
    
        //openAction构造函数,传入一个图标、一个文本和this指针
        openAction = new QAction(QIcon(":/images/doc-open"), tr("&Open..."), this);
        //定义QAction的快捷键
        openAction->setShortcuts(QKeySequence::Open);
        //鼠标置于图标上 出现提示
        openAction->setStatusTip(tr("Open an existing file"));
        connect(openAction, &QAction::triggered, this, &MainWindow::open);
    
        //向菜单栏添加File菜单项  并将QAction对象添加到这个菜单
        QMenu *file = menuBar()->addMenu(tr("&File"));
        file->addAction(openAction);
    
        //增加一个File工具栏  把QAction对象添加到这个工具栏
        QToolBar *toolBar = addToolBar(tr("&File"));
        toolBar->addAction(openAction);
    
        //状态栏 位于底部
        statusBar() ;
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::open()
    {
        QMessageBox::information(this, tr("Information"), tr("Open"));
        //使用对话框Dialog实现
        /*QDialog dialog(this);            //or  QDialog dislog();
        dialog.setWindowTitle(tr("Hello, dialog!"));
        dialog.exec();*/
    }
    //main.cpp
    #include "mainwindow.h"
    #include <QApplication>
    #include <QPushButton>
    #include <QDebug>
    #include <QHBoxLayout>
    
    int main(int argc, char *argv[])
    {
    
        //QCoreApplication app(argc, argv);
        QApplication app(argc,argv);
        MainWindow w;
        w.show();
    
        return app.exec();
    }

    添加图片资源,可以参照教程

    如下图,应该是没有问题的。

    但是,运行后无法加载。

    2. 对话框

    教程地址:https://www.devbean.net/2012/09/qt-study-road-2-dialogs-intro/

    对话框 分为 模态对话框和非模态对话框。

    模态对话框,就是会阻塞同一应用程序中其它窗口的输入,必须等该对话框关闭后,才能对其他窗口进行操作。

    模态对话框分为  应用程序级别的模态 和 窗口级别的模态,默认是应用程序级别的模态。

    应用程序级别的模态是指,——当该种模态的对话框出现时,用户必须首先对对话框进行交互,直到关闭对话框,然后才能访问程序中其他的窗口。

    窗口级别的模态——模态仅仅阻塞与对话框关联的窗口,但是依然允许用户与程序中其它窗口交互。

    Qt 使用QDialog::exec()实现应用程序级别的模态对话框,使用QDialog::open()实现窗口级别的模态对话框,使用QDialog::show()实现非模态对话框。

    注意:使用show()时,对话框一闪而过。因为show()函数不会阻塞当前线程,对话框会显示出来,然后函数立即返回。dialog 是建立在栈上的,show()函数返回,MainWindow::open()函数结束,dialog 超出作用域被析构,因此对话框消失了。

    void MainWindow::open()
    {
        QDialog *dialog = new QDialog;
        dialog->setWindowTitle(tr("Hello, dialog!"));
        dialog->show();
    }
    //关闭对话框时自动销毁对象
    dialog->setAttribute(Qt::WA_DeleteOnClose);

     3. 对话框传递信息

    定义一个dialog,在dialog中输入信息,传递到MainWindow。

    先设计两者的布局,如下:

    代码如下:

    //dialog.h
    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private:
        Ui::Dialog *ui;
    
    signals:
        void  sendData(QString);
    private slots:
        void on_buttonBox_accepted();
    };
    
    #endif // DIALOG_H
    //MainWindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    private:
        Ui::MainWindow *ui;
    
    private slots:
        void receiveData(QString data);
    
    };
    
    #endif // MAINWINDOW_H
    //dialog.cpp
    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QTextEdit>
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_buttonBox_accepted()
    {
        emit sendData(ui->textEdit->toPlainText());
    }
    //mainwindow.cpp
    #include <QAction>
    #include <QMenuBar>
    #include <QMessageBox>
    #include <QStatusBar>
    #include <QToolBar>
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        //信号槽方式下父子窗体传值的测试
        Dialog *dlg = new Dialog;
        //关联信号和槽函数
        connect(dlg,SIGNAL(sendData(QString)),this,SLOT(receiveData(QString)));
       // dlg->setModal(true); 不论是模态或者非模态都可以正常传值
        dlg->show();
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::receiveData(QString data)
    {
        ui->textEdit->setText(data);
    }
  • 相关阅读:
    面向对象
    linux下apache重启报错
    mysql登录密码忘记怎么办?
    html基础知识梳理
    用js实现贪吃蛇
    简单轮播图案例
    JavaScript基础学习笔记整理
    读书笔记之《Redis开发与运维》—— 三
    读书笔记之《Redis开发与运维》—— 二
    读书笔记之《Redis开发与运维》—— 一
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/7172945.html
Copyright © 2011-2022 走看看