Qt学习-文件设置QFileDialog+下拉框QComboBox

目录

一:结果展示

二:代码实现

设置界面类定义如下

具体实现方法如下

构造

UI窗口

界面控件布局--可以对标签文本字体大小设置

本节核心-下拉框实现

本节核心-文件存储路径窗口函数实现

按钮-信号槽机制


一:结果展示

文件存储路径设置

下拉框 

二:代码实现

以设置界面为例

设置界面类定义如下

#ifndef SETWIDGET_H
#define SETWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QButtonGroup>
#include <QComboBox>
#include <QCheckBox>
#include <QIcon>
#include <QMessageBox>
#include <QKeyEvent>
#include<QDebug>
#include <QCameraInfo>
#include <QFileDialog>
#include "decode.h"
#include "setcontrol.h"

class SetWidget : public QWidget
{
    Q_OBJECT
public:
    //    explicit SetWidget(QWidget *parent = 0);
    SetWidget();//构造函数
    void init_UI();//UI窗口
    void init_control();//控件布局
    void init_connect();//按钮-信号槽机制
    int mark;//标志位--有无数据

private:
    QLabel *Set_label,*VideoPath_label,*ImagePath_label,*Interval_label,*Camera_label;//标签文本
    QPushButton *VideoPath_btn,*ImagePath_btn,*OK_btn,*Esc_btn;//按钮控件
    QLineEdit *VideoPath_edit,*ImagePath_edit;//编辑框
    QComboBox *Interval_box,*Camera_box;//下拉框

signals:
    void tomain();

public slots:
    //视频路径选择窗口函数
    void Select_VideoPath();
    //图片路径选择窗口函数
    void Select_ImagePath();
    //设置信息存储函数
    void init_data();
    //跳转到主界面函数
    void getmain();
};

#endif // SETWIDGET_H

具体实现方法如下

构造

//构造函数
SetWidget::SetWidget()
{
    this->mark=0;//标志位--有无数据存储
    init_UI();//窗口
    init_control();//控件布局
    init_connect();//按钮-信号槽机制
}

UI窗口

//UI窗口
void SetWidget::init_UI()
{
    //设置窗口标题
    this->setWindowTitle("安防监控系统");
    //设置窗口大小
    this->setFixedSize(600,400);
}

界面控件布局--可以对标签文本字体大小设置

//设置窗口控件布局函数
void SetWidget::init_control()
{
    /*字体大小  ft1:标题字体大小
              ft2:标签字体大小*/
    QFont ft1,ft2;
    ft1.setPointSize(30);
    ft2.setPointSize(12);

    this->Set_label = new QLabel("设置界面",this);
    this->Set_label->setGeometry(200,30,260,40);
    this->Set_label->setFont(ft1);

    this->VideoPath_label = new QLabel("视频存储位置",this);
    this->VideoPath_label->setGeometry(90,100,120,30);
    this->VideoPath_label->setFont(ft2);

    this->ImagePath_label = new QLabel("图片存储位置",this);
    this->ImagePath_label->setGeometry(90,150,120,30);
    this->ImagePath_label->setFont(ft2);

    this->Interval_label = new QLabel("采集时间间隔",this);
    this->Interval_label->setGeometry(90,200,120,30);
    this->Interval_label->setFont(ft2);

    this->Camera_label = new QLabel("摄像头配置",this);
    this->Camera_label->setGeometry(90,250,120,30);
    this->Camera_label->setFont(ft2);

    this->VideoPath_edit = new QLineEdit(this);
    this->VideoPath_edit->setReadOnly(true);
    this->VideoPath_edit->setGeometry(220,100,200,30);

    this->ImagePath_edit = new QLineEdit(this);
    this->ImagePath_edit->setReadOnly(true);
    this->ImagePath_edit->setGeometry(220,150,200,30);

    this->VideoPath_btn = new QPushButton("...",this);
    this->VideoPath_btn->setGeometry(430,100,30,30);

    this->ImagePath_btn = new QPushButton("...",this);
    this->ImagePath_btn->setGeometry(430,150,30,30);

    //录制时长下拉框控件初始化
    this->Interval_box = new QComboBox(this);
    this->Interval_box->setGeometry(220,200,200,30);
    this->Interval_box->addItem("1分钟");
    this->Interval_box->addItem("2分钟");
    this->Interval_box->addItem("3分钟");
    this->Interval_box->setFont(ft2);

    //摄像头下拉框控件初始化
    this->Camera_box = new QComboBox(this);
    this->Camera_box->setGeometry(220,250,200,30);
    this->Camera_box->setFont(ft2);

    this->OK_btn = new QPushButton("确定",this);
    this->OK_btn->setGeometry(220,300,60,30);
    OK_btn->setFont(ft2);

    this->Esc_btn = new QPushButton("取消",this);
    this->Esc_btn->setGeometry(340,300,60,30);
    Esc_btn->setFont(ft2);
}

本节核心-下拉框实现

#include <QComboBox>
    //录制时长下拉框控件初始化
    this->Interval_box = new QComboBox(this);
    this->Interval_box->setGeometry(220,200,200,30);
    this->Interval_box->addItem("1分钟");
    this->Interval_box->addItem("2分钟");
    this->Interval_box->addItem("3分钟");

本节核心-文件存储路径窗口函数实现

#include <QFileDialog>
//视频路径选择窗口函数
void SetWidget::Select_VideoPath()
{
    QString Video_path=QFileDialog::getExistingDirectory(this,"选择视频存储路径","../");
    if(Video_path != nullptr)
    {
        //显示到编辑框上
        this->VideoPath_edit->setText(Video_path);
    }
}

//图片路径选择窗口函数
void SetWidget::Select_ImagePath()
{
    QString Image_path=QFileDialog::getExistingDirectory(this,"选择视频存储路径","../");
    if(Image_path != nullptr)
    {
        //显示到编辑框上
        this->ImagePath_edit->setText(Image_path);
    }
}

按钮-信号槽机制

//按钮-信号槽机制
void SetWidget::init_connect()
{
    //选择视频路径
    connect(this->VideoPath_btn,SIGNAL(clicked()),this,SLOT(Select_VideoPath()));
    //选择图片路径
    connect(this->ImagePath_btn,SIGNAL(clicked()),this,SLOT(Select_ImagePath()));
    //确定--设置信息存储
    connect(this->OK_btn,SIGNAL(clicked()),this,SLOT(init_data()));
    //确定--跳转到主界面
    connect(this->OK_btn,SIGNAL(clicked()),this,SLOT(getmain()));
}

猜你喜欢

转载自blog.csdn.net/m0_56051805/article/details/125175268