• ubuntu16下用QT5实现对话框应用


    ubuntu16下用QT5,实现对话框程序,步骤:生成界面Dialog.ui,将它应用到主程序,通过主程序显示。

    一 界面练习

    1 Dialog.ui界面生成

      在命令行输入:designer

      进入界面编辑,然后生成MyDialog.ui,下图为本例的界面

    2 将Dialog.ui文件拷贝到当前project下,并“添加现有”到本工程

    3编辑main.cpp文件:

    #include <QDialog>
    #include <QApplication>
    #include <ui_mydialog.h>       //注意将mydialog.ui的名字重命名ui_mydialog.h,此文件暂时没有,不用担心编译不过,预编译会自动将它补上。

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Ui::mydialog ui;
        QDialog *dialog = new QDialog;   // 生成一个普遍的对话框dialog
        ui.setupUi(dialog);                      // 将dialog与当前用户自定义界面绑定
        dialog->show();                         // 输出此对话框
        return a.exec();
    }

    二 让界面有用

      在以上基础上,要想生成自己“有用”界面,需要做如下处理,首先,构建你自己的对话框,它的来历是双继承完成的,即你的对话框又继承界面Dialog.ui

    还继承系统QDialog,让系统消息和界面事件共同起作用。如图:

    构建头文件dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    #include <QDialog>
    #include "ui_dialog.h"
    class myDialog : public QDialog, public Ui::Dialog
    {
    Q_OBJECT
    public:
        myDialog(QWidget *parent = 0);
    private slots:
        void on_lineEdit_textChanged();
    };
    #endif // DIALOG_H

    和实现文件:dialog.cpp

    #include <QtGui>
    #include "dialog.h"
    
    myDialog::myDialog(QWidget *parent)
    : QDialog(parent)
    {
        setupUi(this);
        QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
        lineEdit->setValidator(new QRegExpValidator(regExp, this));
        connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
        connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    }
    
    void myDialog::on_lineEdit_textChanged()
    {
        okButton->setEnabled(lineEdit->hasAcceptableInput());
    }

    修改main.cpp

    #include <QDialog>
    #include <QApplication>
    #include <dialog.h>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        myDialog *dialog = new myDialog;
        dialog->show();
        return a.exec();
    }

    通过以上三个步骤,编译程序,只要点击OK按钮能够有所反应,说明构建基本完成。

  • 相关阅读:
    待测试
    js中substring和substr的用法
    JavaScript lastIndexOf() 方法
    CSS3 :nth-child() 选择器
    jQuery :has() 选择器
    jquery tr:even,tr:eq(),tr:nth-child()区别
    JS模块化工具requirejs教程(二):基本知识
    JS模块化工具requirejs教程(一):初识requirejs
    HTML基础js操作
    HTML基础dom操作
  • 原文地址:https://www.cnblogs.com/gongdiwudu/p/6385645.html
Copyright © 2020-2023  润新知