QT简单实例
- QT简单实例
- 一:通过拖动创建
- 1.创建工程
- 2.拖动控件实现响应
- 3.文件目录
- 3.1 TestQDialog.pro
- 3.2 main.cpp
- 3.3 dialog.h
- 3.4 dialog.cpp
- 二:通过动态创建
- 1.创建工程
- 2.文件目录
- 2.1 TestQDialogSelf.pro
- 2.2 main.cpp
- 2.3 dialog.h
- 2.4 dialog.cpp
QT简单实例
一:通过拖动创建
1.创建工程
1.1 新建项目->Application->Qt Widegets Application
1.2 基类->QDialog
2.拖动控件实现响应
2.1 双击 dialog.ui
2.2 拖动 Display Widgets–Label、Input Widgets–Line Edit、Buttons–Push button
2.3修改控件属性
2.4最后,修改areaLabel2的“frameShape”为Panel;“frameShadow”为Sunken,如图1.19所示。最终效果如图所示
2.5添加控件响应函数
1)方式1:在LineEdit文本框内输入半径值,然后单击“计算”按钮,则在areaLabel 2中显示对应的圆面积。编写代码步骤如下。(1)在“计算”按钮上单击鼠标右键,在弹出的下拉菜单中选择“转到槽…”命令,如图1.22所示。在弹出的对话框中选择“clicked()”信号,如图所示,
2)方式2:在LineEdit内输入半径值,不需要单击按钮触发单击事件,直接就在areaLabel 2中显示圆面积。编写代码步骤如下。(1)在“LineEdit”编辑框上单击鼠标右键,在弹出的下拉菜单中选择“转到槽…”菜单项,在弹出的对话框中选择“textChanged(QString)”信号,如图所示。
3.文件目录
3.1 TestQDialog.pro
#-------------------------------------------------
#
# Project created by QtCreator 2025-04-15T09:43:14
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestQDialogSelf
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
dialog.cpp
HEADERS += \
dialog.h
FORMS += \
dialog.ui
3.2 main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
3.3 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();
protected:
Ui::Dialog *ui;
protected slots:
void on_countBtn_clicked();
private slots:
void on_radiusLineEdit_textChanged(const QString &arg1);
};
#endif // DIALOG_H
3.4 dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//删除标题栏
setWindowFlags(Qt::FramelessWindowHint);
// 背景黑色
//setStyleSheet("background-color: black;");
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_countBtn_clicked()
{
const double PI = 3.1416;
QString str = ui->radiusLineEdit->text();
int nVal = str.toInt();
double dArea = PI*nVal*nVal;
QString strRes;
strRes.setNum(dArea);
ui->areaLabel_2->setText(strRes);
}
void Dialog::on_radiusLineEdit_textChanged(const QString &arg1)
{
const double PI = 3.1416;
int nVal = arg1.toInt();
double dArea = nVal*nVal*PI;
QString strRes;
ui->areaLabel_2->setText(strRes.setNum(dArea));
}
二:通过动态创建
1.创建工程
1.1 新建项目->Application->Qt Widegets Application
1.2 基类->QDialog
2.文件目录
2.1 TestQDialogSelf.pro
#-------------------------------------------------
#
# Project created by QtCreator 2025-04-15T09:43:14
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestQDialogSelf
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
dialog.cpp
HEADERS += \
dialog.h
FORMS += \
dialog.ui
2.2 main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
2.3 dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
protected slots:
void showArea();
void showChangeArea(QString str);
protected:
Ui::Dialog *ui;
protected:
QLabel* pLabel_1;
QLabel* pLabel_2;
QLineEdit* pLineEdit;
QPushButton* pPushButton;
};
#endif // DIALOG_H
2.4 dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QGridLayout>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//去掉标题栏
setWindowFlags(Qt::FramelessWindowHint);
//创建控件
pLabel_1 = new QLabel(this);
pLabel_1->setText("请输入圆的半径:");
pLabel_2 = new QLabel(this);
pLineEdit = new QLineEdit(this);
pPushButton = new QPushButton(this);
pPushButton->setText("显示对应圆的面积");
//创建容器
QGridLayout* mainLayout = new QGridLayout(this);
mainLayout->addWidget(pLabel_1,0,0);
mainLayout->addWidget(pLineEdit,0,1);
mainLayout->addWidget(pLabel_2,1,0);
mainLayout->addWidget(pPushButton,1,1);
//绑定控件响应函数
connect(pPushButton,SIGNAL(clicked()),this,SLOT(showArea()));
connect(pLineEdit,SIGNAL(textChanged(QString)),this,SLOT(showChangeArea(QString)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showArea()
{
const double PI = 3.1416;
QString str = pLineEdit->text();
int nVal = str.toInt();
double dArea = PI*nVal*nVal;
QString strRes;
strRes.setNum(dArea);
pLabel_2->setText(strRes);
}
void Dialog::showChangeArea(QString str)
{
const double PI = 3.1416;
int nVal = str.toInt();
double dArea = PI*nVal*nVal;
QString strRes;
strRes.setNum(dArea);
pLabel_2->setText(strRes);
}