【Qt学习】 QFileDialog、QComboBox 的使用

目录

一:效果展示

二:源码分享


一:效果展示

文件存储路径设置

下拉框 

二:源码分享

以设置界面为例

设置界面类定义如下

#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()));
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenruhan_QAQ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值