Qt学习第三天:QStackedWIdget 堆栈窗体,打开文件,打印文件路径和内容

Qt学习第三天:QStackedWIdget 堆栈窗体,打开文件,打印文件路径和内容-CSDN博客

堆栈窗体的意思是,将多个窗口叠加,同一时间,用户只能看到一个窗口。

一,实现效果

点击左侧选项右边窗口切换

widget.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QStackedWidget>
#include <QHBoxLayout>
#include <QListWidget>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = nullptr);
~Widget();

public slots:
void testSlot(int index);

private:
Ui::Widget *ui;
// 三个窗口
QWidget* first_widget;
QWidget* second_widget;
QWidget* third_widget;
// 堆栈窗体对象
QStackedWidget* stacked_widget;
// 布局对象
QHBoxLayout* hboxlayout;
// 表格选项
QListWidget* list_widget;
};
#endif // WIDGET_H

widget.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);

// 实例化三个窗口对象
first_widget = new QWidget;
second_widget = new QWidget;
third_widget = new QWidget;

// 通过样式表修改三个窗口的背景颜色
first_widget->setStyleSheet("background-color:rgb(255, 0, 0)");
second_widget->setStyleSheet("background-color:rgb(0, 255, 0)");
third_widget->setStyleSheet("background-color:rgb(0, 0, 255)");

// 堆栈窗体对象按照顺序添加三个窗口
// 当堆栈窗体对象调用addWidget添加窗口时,会给窗口自动添加一个以0为开始自增的index值
stacked_widget = new QStackedWidget(this);
stacked_widget->addWidget(first_widget); // index:0
stacked_widget->addWidget(second_widget);// index:1
stacked_widget->addWidget(third_widget); // index:2

// 实例化表格选项,添加三个选项,分别代表三个窗口
list_widget = new QListWidget(this);
list_widget->addItem("red widget"); // index:0
list_widget->addItem("green widget");// index:1
list_widget->addItem("blue widget");// index:2

// 设置持续的布局,将list_widget和stacked_widget添加到布局里
hboxlayout = new QHBoxLayout(this);
hboxlayout->addWidget(list_widget);
hboxlayout->addWidget(stacked_widget);

// 设置比例关系为1:5
hboxlayout->setStretchFactor(list_widget, 1);
hboxlayout->setStretchFactor(stacked_widget, 5);

// 连接信号和槽,通过点击选项,就可以跳转到堆栈窗体里的不同对象
connect(list_widget, SIGNAL(currentRowChanged(int)),
this, SLOT(testSlot(int)));
connect(list_widget, SIGNAL(currentRowChanged(int)),
stacked_widget, SLOT(setCurrentIndex(int)));
}
Widget::~Widget()
{
delete ui;
}
// 测试currentRowChanged信号传来的index值
void Widget::testSlot(int index)
{
qDebug() << "index:" << index;
}

二,在上述窗口案例的基础上实现功能:

1,红色窗口可以点击按钮,并弹出打开文本文件的对话框。

widget.h定义

1
2
3
4
5
6
7
8
#include <QPushButton>
#include <QFileDialog>
// ...
public slots:
void buttonSlot();
// ...
private:
QPushButton* button;

widget.cpp按钮样式与布局

1
2
3
4
5
6
7
// 控件在实例化时,一般来说在构造函数里,添加指定的窗口,之前都是填写this,这一次我们也可以将控件放置在独立的其他窗口上。
button = new QPushButton("Select File", first_widget);
button->setMinimumHeight(80);
button->setStyleSheet("font:35px");
// 在堆栈窗体的子窗口上,添加布局。
QHBoxLayout* first_layout = new QHBoxLayout(first_widget);
first_layout->addWidget(button);

widget.cpp 打开文件的槽函数
鼠标点击QFileDialog按F1可以查看相应的文档

1
2
3
4
5
6
7
8
void Widget::buttonSlot()
{
// 使用QFileDialog打开Qt的文件对话框
// 如果点击了取消cancel,则返回值为空字符串
// 如果点击了指定的文件,则将返回该文件的绝对路径
QString file_name = QFileDialog::getOpenFileName(this,
tr("Open File"), "/home", tr("text Files (*.c *.cpp *.h *.txt)"));
}

2,选中文件的话,在绿色窗口打印文件的绝对路径。

widget.h定义

1
2
3
4
5
#include <QTextEdit>
#include <QMessageBox>
// ...
private:
QLineEdit* edit;

widget.cpp文本框样式与布局

1
2
3
4
5
edit = new QLineEdit(second_widget);
edit->setMinimumHeight(80);
edit->setStyleSheet("font:35px");
QHBoxLayout* second_layout = new QHBoxLayout(second_widget);
second_layout->addWidget(edit);

widget.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Widget::buttonSlot()
{
QString file_name = QFileDialog::getOpenFileName(this,
tr("Open File"), "/home", tr("text Files (*.c *.cpp *.h *.txt)"));
// 判断字符串是否为空串
if(!file_name.isEmpty())
{
//文件的绝对路径file_name
edit->setText(file_name);
}
else
{
// Qt的消息盒子,提供了方法可以弹出一个小的提示对话框。
QMessageBox::information(this, "information", "Open file cancel!");
return;
}
}

3,在第二步的基础上,在蓝色窗口打印该文件的所有内容。

widget.h定义

1
2
3
4
5
#include <QTextEdit>
#include <QFile>
// ...
private:
QTextEdit* text_edit;

widget.cpp文本框样式与布局

1
2
3
text_edit = new QTextEdit(third_widget);
QHBoxLayout* third_layout = new QHBoxLayout(third_widget);
third_layout->addWidget(text_edit);

widget.cpp槽函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void Widget::buttonSlot()
{
QString file_name = QFileDialog::getOpenFileName(this,
tr("Open File"), "/home", tr("text Files (*.c *.cpp *.h *.txt)"));
if(!file_name.isEmpty())
{
edit->setText(file_name);
}
else
{
QMessageBox::information(this, "information", "Open file cancel!");
return;
}
// Qt的文件读写操作,都是可以利用QFile对象实现的。QFile类似于C语言当中FILE*指针
// 实例化QFIle对象时,需要指定文件的绝对路径。
QFile file(file_name);
// 利用open函数打开指定路径的文件,有可能会打开失败,需要提取判断返回值的bool数值
// 打开的模式,需要选择具体的读写方式,这里只设计只读操作,所以用QIODevice::ReadOnly即可。
if(!file.open(QIODevice::ReadOnly))
{
text_edit->append("open file error");
return;
}
// 打开文件成功之后,利用readAll读取该文件的所有内容,并且返回一个QByteArray类型的数据。
// QByteArray意思是字节数组。每个元素的大小为1字节,所以也可以看成是unsigned char[]数组的二次封装。
QByteArray data = file.readAll();
// 将所有的文本信息添加到textEdit里面去。
text_edit->setText(data);
// 结束代码逻辑时关闭文件。
file.close();
}



Qt学习第三天:QStackedWIdget 堆栈窗体,打开文件,打印文件路径和内容
https://leaf-domain.gitee.io/2024/01/05/qt3/
作者
叶域
发布于
2024年1月5日
许可协议