5.常用GUI部件

一、了解QT常用的部件类
QPushButton:按钮
QRadioButton:单选框
QCheckBox:多选框
QListWidget:列表部件
QTableWidget:表格组件
QGroupBox: 组框
QStackedWidget:堆栈窗体
QWidget:基础窗口部件
QComboBox:组合框
QLineEdit:单行文本框
QTextEdit:多行文本框
QTimeEdit:时间编辑框
QSpinBox:微调框
QSlider:滑动条
QLabel:标签	
QProgressBar:进度条

实例:
界面内容为:进度条、两个按钮+/-
功能:点击+/-按钮后实现进度条变化并显示当前进度值,全部用代码实现。
补充:增加图片,借助qt的资源文件,资源文件提供管理资源的方法,并且把资源编译到可执行文件中,和路径无关。
方法:新建-->QT-->QT资源文件(提供图片资源)

二、内建对话框使用	
	这些对话框帮助文档要关注 Static Public Members 部分,参考帮助文档demo使用即可。
1.QFileDialog
	QString	getOpenFileName(QWidget * parent = 0, const QString & caption = QString(), \
	const QString & dir = QString(), const QString & filter = QString(), \
	QString * selectedFilter = 0, Options options = 0)
	
	作用:打开文件对话框
	参数:
		parent:父窗口,一般填this
		caption:弹出窗口的标题
		dir:默认打开目录
		filter:过滤条件
		剩余默认
	返回值:打开文件路径或者空
	
2.实例:界面加入一个按钮和一个文本编辑框,点击按钮后弹出文件选择窗口,
选择文件后把文件绝对路径增加到文本编辑框内。
取消选择弹出对话框提示“打开失败”。

3.其它对话框:QMessageBox、QInputDialog、QColorDialog、QFontDialog
在这里插入图片描述

#include "progresswig.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ProgressWig w;
    w.show();

    return a.exec();
}

#-------------------------------------------------
#
# Project created by QtCreator 2018-05-08T21:22:55
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = progress
TEMPLATE = app


SOURCES += main.cpp\
        progresswig.cpp

HEADERS  += progresswig.h

RESOURCES += \
    res.qrc

#include "progresswig.h"

ProgressWig::ProgressWig(QWidget *parent)
    : QWidget(parent)
{
    this->setFixedSize(300, 300);  //设置固定大小

    addBtn = new QPushButton(this);
    addBtn->setIcon(QIcon("://pic/add.png"));
    addBtn->setFixedSize(50, 50);

    subBtn = new QPushButton(this);
    subBtn->setIcon(QIcon("://pic/sub.png"));
    subBtn->setFixedSize(50, 50);

    bar = new QProgressBar(this);
    bar->setFixedSize(150, 50);

    addBtn->move(10, 100);
    subBtn->move(240, 100);
    bar->move(70, 100);

    val = 50;
    bar->setValue(val);

    QObject::connect(addBtn, SIGNAL(clicked()),
                     this,  SLOT(addSlot()));

    QObject::connect(subBtn, SIGNAL(clicked()),
                     this,  SLOT(subSlot()));
}

ProgressWig::~ProgressWig()
{

}

void ProgressWig::addSlot()
{
    val++;
    bar->setValue(val);
}

void ProgressWig::subSlot()
{
    val--;
    bar->setValue(val);
}



#ifndef PROGRESSWIG_H
#define PROGRESSWIG_H

#include <QtWidgets>

class ProgressWig : public QWidget
{
    Q_OBJECT

public:
    ProgressWig(QWidget *parent = 0);
    ~ProgressWig();

private:
    QPushButton *addBtn;
    QPushButton *subBtn;
    QProgressBar *bar;

    int val;

private slots:
    void addSlot();
    void subSlot();
};

#endif // PROGRESSWIG_H

发布了10 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40083589/article/details/94025599
今日推荐