文章目录
QT学习
QT基础
基础操作
创建按钮并调整
#include <QPushButton>//调用按钮头文件
QPushButton *bt;
this->setFixedSize(640,480);//窗口大小
bt = new QPushButton("登录",this);
bt->setGeometry(300,400,100,50);//按钮坐标和大小
//按钮字体颜色
bt->setStyleSheet("QPushButton{color:red;};");
//设置风格;设置登录框背景显示蓝色
bt->setStyleSheet("QPushButton{background-color:blue;};");
//设置风格;设置登录可以根据颜色码表设置颜色蓝色
//bt->setStyleSheet("QPushButton{background-color:#63B8FF;};");
显示对话框
#include <QPushButton>//调用按钮头文件
#include <QLineEdit> //行编辑器
QPushButton *bt;
QLineEdit *le; //行编辑器
#include"QVBoxLayout"// 排版的垂直类
//1.在堆区真正的将对象申请出来
bt = new QPushButton("登录",this);//构造一个按钮对象,并将其放置在当前的主界面上
le = new QLineEdit;//构造一个对话框
//2.做必要的排版
QVBoxLayout *vbox = new QVBoxLayout;//垂直布局,用于将按钮和行编辑框进行垂直排列
vbox->addWidget(le);//行编辑框,使其成为布局中的另一个元素。
vbox->addWidget(bt);//按钮,使其成为布局中的另一个元素。
this->setLayout(vbox);//将垂直布局管理器设置为当前主界面的布局,显示当前主界面
点击按钮电脑自动播报语音
.por
QT += core gui texttospeech
#include <QWidget>
#include <QPushButton>//调用按钮头文件
#include <QLineEdit> //行编辑器
#include <QDebug>//打印跟踪
#include <QTextToSpeech> //说话的类 需要在pro加入语言转换类
class Widget : public QWidget
{
Q_OBJECT
/*读*/
public slots://申明该函数为槽函数(能被信号激发);
void xxx()
{
x->say(le->text()); //说话的类,编辑框中的文字
qDebug() << le->text(); //显示打印xxxxxxxxxxxx
}
public:
Widget(QWidget *parent = nullptr);
~Widget();
QPushButton *bt;
QLineEdit *le; //行编辑器
QTextToSpeech *x; //说话的类
};
#endif // WIDGET_H
//1.在堆区真正的将对象申请出来
bt = new QPushButton("读",this);//构造一个按钮对象,并将其放置在当前的主界面上
le = new QLineEdit;//构造一个对话框
x = new QTextToSpeech;
//2.做必要的排版
QVBoxLayout *vbox = new QVBoxLayout;//垂直布局,用于将按钮和行编辑框进行垂直排列
vbox->addWidget(le);//行编辑框,使其成为布局中的另一个元素。
vbox->addWidget(bt);//按钮,使其成为布局中的另一个元素。
this->setLayout(vbox);//将垂直布局管理器设置为当前主界面的布局,显示当前主界面
//3. 前后台挂接(连接信号与槽)
connect(bt, SIGNAL(clicked(bool)), this, SLOT(xxx())); //按钮连接的信号;如果按钮按下了那么下面xxx应该点击执行
水平布局 || 网格状布局
#include <QHBoxLayout> //排版 水平布局
#include <QGridLayout> //排版 网格状布局
bt = new QPushButton("读",this);
le = new QLineEdit;
x = new QTextToSpeech;
//QHBoxLayout *hbox = new QHBoxLayout; //水平布局
//hbox->addWidget(le); //行编辑框
//hbox->addWidget(bt); //按钮
//this->setLayout(hbox);
QGridLayout *gbox = new QGridLayout; //网格状布局
gbox->addWidget(le, 0, 0, 1, 2); //行编辑框
gbox->addWidget(bt, 1, 1, 1, 1); //按钮
setLayout(gbox);
多个按钮控制
QPushButton *xbt = static_cast<QPushButton *>(sender());//提取发信号的对象
qDebug()<<xbt->text();
x->say(le->text());
QPushButton *bt;
QPushButton *bt1;
bt1 = new QPushButton("我在",this);
gbox->addWidget(bt1, 2, 1, 1, 1);
connect(bt1, SIGNAL(clicked(bool)), this, SLOT(xxx()));
图形编程
对应好别看错就可以
信号和槽(重要)
回调( callback)是一个函数指针,当一个事件发生时被调用,任何函数都可以被安排作为回调
Qt采用信号和槽实现对象部件之间的通信。
信号
-
当信号被发射时,QT代码将回调与其相连接的槽函数
-
信号将由元对象处理moc自动翻译成C++代码
-
信号的声明不在cpp文件中,而在头文件中
槽函数
- 槽函数是普通的C++成员函数,可以被正常调用
- 槽函数可以有返回值,也可以没有
- 槽函数的访问权限三种: public slots、 private slots和 protected slots槽函数的存取权限决定了谁能够与其相关联
发送信号
连接 connect 四个参数
connect(信号的发送者,发送的具体信号,信号的接收者,信号的处理(槽))
connect(btn,&QPushButton::clicked,this,&QWidget::close);
信号槽的优点,松散耦合, 信号的发送端 和 接受端本身是没有关联的,通过connect连接将两端耦合到一起
emit mySignal(); 发送信号
计算器实践
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <Qstring>
#include <QLineEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QLineEdit ant;
QString a;
QString b;
QString F;
double sum = 0;
int flag = 0;
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
void on_btn3_clicked();
void on_btn4_clicked();
void on_btn5_clicked();
void on_btn6_clicked();
void on_btn7_clicked();
void on_btn8_clicked();
void on_btn9_clicked();
void on_mul_clicked();
void on_btn0_clicked();
void on_div_clicked();
void on_add_clicked();
void on_sub_clicked();
void on_eq_clicked();
void on_de_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("Aoiner的计算器");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btn1_clicked()
{
if(flag == 0)
{
a += '1';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '1';
ui->ant->setText(b);
}
}
void Widget::on_btn2_clicked()
{
if(flag == 0)
{
a += '2';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '2';
ui->ant->setText(b);
}
}
void Widget::on_btn3_clicked()
{
if(flag == 0)
{
a += '3';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '3';
ui->ant->setText(b);
}
}
void Widget::on_btn4_clicked()
{
if(flag == 0)
{
a += '4';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '4';
ui->ant->setText(b);
}
}
void Widget::on_btn5_clicked()
{
if(flag == 0)
{
a += '5';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '5';
ui->ant->setText(b);
}
}
void Widget::on_btn6_clicked()
{
if(flag == 0)
{
a += '6';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '6';
ui->ant->setText(b);
}
}
void Widget::on_btn7_clicked()
{
if(flag == 0)
{
a += '7';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '7';
ui->ant->setText(b);
}
}
void Widget::on_btn8_clicked()
{
if(flag == 0)
{
a += '8';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '8';
ui->ant->setText(b);
}
}
void Widget::on_btn9_clicked()
{
if(flag == 0)
{
a += '9';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '9';
ui->ant->setText(b);
}
}
void Widget::on_btn0_clicked()
{
if(flag == 0)
{
a += '0';
ui->ant->setText(a);
}
if(flag == 1)
{
b += '0';
ui->ant->setText(b);
}
}
void Widget::on_mul_clicked()
{
if(flag == 1)
{
if(F=='*')
{
sum = a.toDouble() * b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
b.clear();
}
}
else
{
flag = 1;
F = '*';
ui->ant->setText(F);
}
}
void Widget::on_div_clicked()
{
if(flag == 1)
{
if(F=='/')
{
sum = a.toDouble() / b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
b.clear();
}
}
else
flag = 1;
F = '/';
ui->ant->setText(F);
}
void Widget::on_add_clicked()
{
if(flag == 1)
{
if(F=='+')
{
sum = a.toDouble() + b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
b.clear();
}
}
flag = 1;
F = '+';
ui->ant->setText(F);
}
void Widget::on_sub_clicked()
{
if(flag == 1)
{
if(F=='-')
{
sum = a.toDouble() - b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
b.clear();
}
}
flag = 1;
F = '-';
ui->ant->setText(F);
}
void Widget::on_eq_clicked()
{
if(flag == 1)
{
if(F=='+')
{
sum = a.toDouble() + b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
flag = 0;
b.clear();
}
else if(F=='-')
{
sum = a.toDouble() - b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
flag = 0;
b.clear();
}
else if(F=='*')
{
sum = a.toDouble() * b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
flag = 0;
b.clear();
}
else if(F=='/')
{
sum = a.toDouble() / b.toDouble();
a = QString::number(sum);
ui->ant->setText(a);
flag = 0;
b.clear();
}
}
}
void Widget::on_de_clicked()
{
a.clear();
b.clear();
F.clear();
flag = 0;
ui->ant->setText("请输入");
}
单独使用.exe
build…的debug中将.exe单独拿出来
放在文件夹中,复制文件路径
打开QT的MinGW的黑框
C:进入c盘 >>> cd 路径 >>> 输入
windeployqt.exe 自己的exe文件名
窗口部件
对话框(QT组合部件)
widget.h
#include <QWidget>
#include <QPushButton>
#include <QTextEdit>
#include <QErrorMessage>
Widget(QWidget *parent = nullptr);
QPushButton *bt_filename;
QPushButton *bt_getcolor;
QPushButton *bt_getfont;
QPushButton *bt_getinput;
QPushButton *bt_message;
QPushButton *bt_error;
QPushButton *bt_progress;
QTextEdit *te_test;
widget.cpp
#include "widget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QErrorMessage>
#include <QMessageBox>
#include <QProgressDialog>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
bt_filename = new QPushButton("获取文件名");
bt_getcolor = new QPushButton("获取颜色");
bt_getfont = new QPushButton("获取字体");
bt_getinput = new QPushButton("获取输入");
bt_error = new QPushButton("错误弹框");
bt_message = new QPushButton("消息弹框");
bt_progress = new QPushButton("进度条对话框");
te_test = new QTextEdit;
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(bt_filename);
vbox->addWidget(bt_getcolor);
vbox->addWidget(bt_getfont);
vbox->addWidget(bt_getinput);
vbox->addWidget(bt_error);
vbox->addWidget(bt_message);
vbox->addWidget(bt_progress);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addLayout(vbox);
hbox->addWidget(te_test);
setLayout(hbox);
//文件对话框示例
connect(bt_filename, &QPushButton::clicked, [&](){
// 提取单个文件名的对话框
// QString filename = QFileDialog::getOpenFileName();
// te_test->append(filename);
// 提取多个文件名的对话框
QStringList filenames = QFileDialog::getOpenFileNames(this, "打开图片", ".", "Images (*.png *.xpm *.jpg)");
for(int i=0; i<filenames.length(); i++)
te_test->append(filenames[i]);
});
//颜色对话框示例
connect(bt_getcolor, &QPushButton::clicked, [&](){
QColor color = QColorDialog::getColor();
te_test->setTextColor(color);
});
//字体对话框示例
connect(bt_getfont, &QPushButton::clicked, [&](){
bool ok;
QFont font = QFontDialog::getFont(&ok);
if(ok) //用户选择了字体
te_test->setCurrentFont(font);
});
//输入对话框示例
connect(bt_getinput, &QPushButton::clicked, [&](){
QString str = QInputDialog::getText(this, "xxxx", "yyyy");
te_test->setText(str);
});
//消息弹窗示例
connect(bt_message, &QPushButton::clicked, [&](){
QMessageBox::warning(this, "xxxx", "yyyyyyy", QMessageBox::Open, QMessageBox::Apply);
});
//错误弹窗示例
connect(bt_error, &QPushButton::clicked, [&](){
QErrorMessage *x=new QErrorMessage;;
x->showMessage("xxxxxxxxxxxxxxxxxxx");
});
//进度条对话框示例
connect(bt_progress, &QPushButton::clicked, [&](){
QProgressDialog x;
x.setValue(88);
x.exec();
});
}
常见按钮
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QToolButton>
#include <QRadioButton>
#include <QCheckBox>
class Widget : public QWidget
{
Q_OBJECT
public slots:
// void textbutton();用lamda表达式则不需要声明
void check_pushed(bool);
public:
Widget(QWidget *parent = nullptr);
QPushButton *bt_test;
//工具栏按钮
QToolButton *bt_tool;
//单选按钮
QRadioButton *bt_radio1, *bt_radio2;
//复选按钮
QCheckBox *bt_check1, *bt_check2;
~Widget();
};
#endif // WIDGET_H
#include "widget.h"
#include <QVBoxLayout>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//普通按钮示例
bt_test = new QPushButton("冲锋");
bt_test->setMinimumSize(200, 100); //设置最小尺寸
bt_test->setFlat(true); //设置无边框
bt_test->setIconSize(QSize(80, 80)); //设置按钮图标大小
bt_test->setIcon(QIcon("1.png")); //设置按钮图标
bt_tool = new QToolButton;
bt_tool->setIcon(QIcon("2.png"));
bt_tool->setIconSize(QSize(30, 30));//设置按钮图标
bt_tool->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_M));//绑定快捷键
// connect(bt_tool, SIGNAL(clicked(bool)), this, SLOT(textbutton()));
connect(bt_tool, &QToolButton::clicked, [&](){//lamda表达式(无名函数)
qDebug()<<"yyyyyyyyyyyyyyyyyy";
});
//单选按钮示例
bt_radio2 = new QRadioButton("劈");
bt_radio1 = new QRadioButton("砍");
bt_check1 = new QCheckBox("显示密码");
bt_check2 = new QCheckBox("自动登录");
connect(bt_check1, SIGNAL(clicked(bool)), this, SLOT(check_pushed(bool)));
//垂直布局
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(bt_tool);
vbox->addWidget(bt_test);
vbox->addWidget(bt_radio2);
vbox->addWidget(bt_radio1);
vbox->addWidget(bt_check1);
vbox->addWidget(bt_check2);
setLayout(vbox);
}
//void Widget::textbutton()
//{
// qDebug()<<"xxxxxxxxxxxxxxxx";
//}
void Widget::check_pushed(bool x)
{
qDebug()<<"mmmmmmmmmmmmmmmmm"<<x;
}
Widget::~Widget()
{
}
输入部件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLineEdit>
#include <QCheckBox>
#include<QComboBox>
#include <QFontComboBox>
#include <QTextEdit>
#include <QSpinBox>
#include <QLCDNumber>
#include <QDial>
#include <QScrollBar>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
//行编辑框
QLineEdit *le_test;
QCheckBox *ck_test;
//组合框
QComboBox *cmb_test;
//字体选择下拉框
QFontComboBox *fcb_test;
//文本编辑框
QTextEdit *te_test;
//自旋框
QSpinBox *sp_test;
QLCDNumber *lcd_test;
//旋钮
QDial *dl_test;
//滚动条
QScrollBar *sbr_test;
~Widget();
};
#endif // WIDGET_H
#include "widget.h"
#include <QVBoxLayout>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
le_test = new QLineEdit;
ck_test = new QCheckBox("显示密码");
le_test->setEchoMode(QLineEdit::Password);
connect(ck_test, &QCheckBox::clicked, [&](bool x){
le_test->setEchoMode(x?QLineEdit::Normal:QLineEdit::Password);
});
//组合框/下拉框使用示例
cmb_test = new QComboBox;
cmb_test->addItem("com1");
cmb_test->addItem("com2");
cmb_test->addItem("com3");
connect(cmb_test, &QComboBox::currentTextChanged, [&](QString x){
qDebug() <<x;
});
//文本编辑框(多行输入)使用示例
te_test = new QTextEdit;
//字体输入框使用示例
fcb_test = new QFontComboBox;
connect(fcb_test, &QFontComboBox::currentFontChanged, [&](QFont x){
le_test->setFont(x); //设置字体
//te_test->setFont(x); //设置字体
te_test->setCurrentFont(x); //设置选中文字的字体
});
//自旋框
sp_test = new QSpinBox;
lcd_test = new QLCDNumber;
lcd_test->setMinimumHeight(50);
connect(sp_test, SIGNAL(valueChanged(int)), lcd_test, SLOT(display(int)));
//旋钮使用示例
dl_test = new QDial;
connect(dl_test, SIGNAL(valueChanged(int)), lcd_test, SLOT(display(int)));
//滚动条使用示例
sbr_test = new QScrollBar;
sbr_test->setOrientation(Qt::Horizontal); //设置水平方向
connect(sbr_test, SIGNAL(valueChanged(int)), lcd_test, SLOT(display(int)));
//布局
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(le_test);
vbox->addWidget(ck_test);
vbox->addWidget(cmb_test);
vbox->addWidget(fcb_test);
vbox->addWidget(te_test);
vbox->addWidget(sp_test);
vbox->addWidget(lcd_test);
vbox->addWidget(dl_test);
vbox->addWidget(sbr_test);
setLayout(vbox);
}
Widget::~Widget()
{
}
输出部件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QMovie>
#include <QTextBrowser>
#include <QCalendarWidget>
#include <QLCDNumber>
#include <QProgressBar>
#include <QTimer> //定时器
class Widget : public QWidget
{
Q_OBJECT
public:
//标签(文字提示, 图片显示,动画显示。。。)
QLabel *lb_text;
QLabel *lb_pix;
QLabel *lb_gif;
//文本浏览器(解释html5)
QTextBrowser *tb_test;
//日历
QCalendarWidget *cl_test;
//七段数码管
QLCDNumber *lcd_test;
//进度条
QProgressBar *pbr_test;
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H
#include "widget.h"
#include<QVBoxLayout>
#include <QDate>
#include <QDebug>
#include <QTimer>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//标签显示文字示例
lb_text = new QLabel("我是文字");
lb_text->setAlignment(Qt::AlignCenter); //设置对齐方式
//标签显示图片示例
lb_pix = new QLabel("我是图片");
lb_pix->setScaledContents(true); //设置标签自动缩放上面的图片
lb_pix->setPixmap(QPixmap("Qt.jpg")); //标签显示图片
lb_pix->setFixedSize(200, 120);
//标签显示动画示例
lb_gif = new QLabel;
lb_gif->setFixedSize(200, 120);
lb_gif->setScaledContents(true);
lb_gif->setAlignment(Qt::AlignCenter);
QMovie *m = new QMovie("1.gif"); //标签显示动画
lb_gif->setMovie(m);
m->start();
//文本浏览器显示html文本
tb_test = new QTextBrowser;
tb_test->setHtml("<!DOCTYPE html>\
<html>\
<head>\
<meta charset=\"utf-8\">\
<title>菜鸟教程(runoob.com)</title>\
</head>\
<body>\
<h1>我的第一个标题</h1>\
<p>我的第一个段落。</p>\
</body>\
</html>");
lb_pix->setFixedSize(200, 120);
lb_gif->setFixedSize(200, 120);
lb_gif->setScaledContents(true);
//7段数码管显示示例
lcd_test = new QLCDNumber;
lcd_test->setDigitCount(20);
lcd_test->setMinimumSize(400, 100);
lcd_test->display(1234);
//日历示例
cl_test = new QCalendarWidget;
connect(cl_test, &QCalendarWidget::clicked, [&](QDate x){
qDebug() << x;
lcd_test->display(x.toString());
});
//进度条示例
pbr_test = new QProgressBar;
//pbr_test->setValue(60);
QTimer *t = new QTimer; //定时器类
connect(t, &QTimer::timeout, [&](){ //捕获定时器timeout信号
static int x = 0;
pbr_test->setValue(x++); //在进度条上显示一个数据
lcd_test->display(x); //在7段数码管上显示一个数据
});
t->start(100);
//布局
QVBoxLayout *vbox1 = new QVBoxLayout;
vbox1->addWidget(lb_text);
vbox1->addWidget(lb_pix);
vbox1->addWidget(lb_gif);
vbox1->addWidget(tb_test);
vbox1->addWidget(cl_test);
vbox1->addWidget(lcd_test);
vbox1->addWidget(pbr_test);
setLayout(vbox1);
}
Widget::~Widget()
{
}
容器类
GroupBox-组合框
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QGroupBox>
class Widget : public QWidget
{
Q_OBJECT
public:
QGroupBox *gb1;
QGroupBox *gb2;
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H
#include "widget.h"
#include <QRadioButton> //容器
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//排版的垂直类
//第一个容器
QGroupBox *gb1 = new QGroupBox(tr("Exclusive Radio Buttons"));
QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
radio1->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addStretch(1);
gb1->setLayout(vbox);
//第二个容器
QGroupBox *gb2 = new QGroupBox(tr("Exclusive Radio Buttons"));
QRadioButton *radio11 = new QRadioButton(tr("&Radio button 11"));
QRadioButton *radio12 = new QRadioButton(tr("R&adio button 12"));
QRadioButton *radio13 = new QRadioButton(tr("Ra&dio button 13"));
radio11->setChecked(true);
QVBoxLayout *vbox1 = new QVBoxLayout;
vbox1->addWidget(radio11);
vbox1->addWidget(radio12);
vbox1->addWidget(radio13);
vbox1->addStretch(1);
gb2->setLayout(vbox1);
QVBoxLayout *mainbox = new QVBoxLayout;
mainbox->addWidget(gb1);
mainbox->addWidget(gb2);
this->setLayout(mainbox);
}
Widget::~Widget()
{
}
Scroll Area-滚动窗口容器
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QLabel *lb_pix;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QScrollArea>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
lb_pix = new QLabel;
lb_pix->setPixmap(QPixmap("1.png"));
// lb_pix->setFixedSize(640, 480);
QScrollArea *sl = new QScrollArea; //滚动区域容器
sl->setFixedSize(640, 480);
sl->setWidget(lb_pix); //将其他对象放入此容器
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(sl);
setLayout(vbox);
}
Widget::~Widget()
{
}
Tool Box-工具箱
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QTextEdit *te_test1;
QTextEdit *te_test2;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include <QToolBox>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
te_test1 = new QTextEdit("xxxxxxxxxxxxxxxxx\nxxxxxxxxxx\nxxxx");
te_test2 = new QTextEdit("yyyyyyyyyyyyy\nyyyyyyyyyy\nyyyyyyyyyy");
QToolBox *tb = new QToolBox;
tb->addItem(te_test1, "xxxxx1");
tb->addItem(te_test2, "xxxxx2");
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(tb);
setLayout(vbox);
}
Widget::~Widget()
{
}
TabWidget-选项卡-分页显示
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QTextEdit *te_test1;
QTextEdit *te_test2;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include <QTabWidget>
#include <QVBoxLayout>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
te_test1 = new QTextEdit("xxxxxxxxxxxxxxxxx\nxxxxxxxxxx\nxxxx");
te_test2 = new QTextEdit("yyyyyyyyyyyyy\nyyyyyyyyyy\nyyyyyyyyyy");
QTabWidget *tb = new QTabWidget;
tb->setTabsClosable(true);
tb->addTab(te_test1, "1.c");
tb->addTab(te_test2, "2.c");
connect(tb, &QTabWidget::tabCloseRequested, [&](int x){
qDebug()<<"xxx"<<x;
});
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(tb);
setLayout(vbox);
}
Widget::~Widget()
{
}
QStackedWidget-堆栈窗口类
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
#include <QComboBox>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QTextEdit *te_test1;
QTextEdit *te_test2;
QComboBox *cb_test;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include <QStackedWidget>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
te_test1 = new QTextEdit("xxxxxxxxxxxxxxxxx\nxxxxxxxxxx\nxxxx");
te_test2 = new QTextEdit("yyyyyyyyyyyyy\nyyyyyyyyyy\nyyyyyyyyyy");
QStackedWidget *tb = new QStackedWidget;
tb->addWidget(te_test1);
tb->addWidget(te_test2);
cb_test = new QComboBox;
cb_test->addItem("1.c");
cb_test->addItem("2.c");
connect(cb_test, SIGNAL(activated(int)), tb, SLOT(setCurrentIndex(int)));
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(cb_test);
vbox->addWidget(tb);
setLayout(vbox);
}
Widget::~Widget()
{
}
MdiArea-多媒体窗口
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QTextEdit *te_test1;
QTextEdit *te_test2;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include <QMdiArea>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
te_test1 = new QTextEdit("xxxxxxxxxxxxxxxxx\nxxxxxxxxxx\nxxxx");
te_test2 = new QTextEdit("yyyyyyyyyyyyy\nyyyyyyyyyy\nyyyyyyyyyy");
QMdiArea *mdi = new QMdiArea;
mdi->addSubWindow(te_test1);
mdi->addSubWindow(te_test2);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(mdi);
setLayout(vbox);
}
Widget::~Widget()
{
}