Qt 5.14.2 学习记录 —— 십삼 QComboBox、QSpinBox、QDateTimeEdit、QDial、QSlider

文章目录


1、QComboBox

下拉框


信号

写程序来查看各个功能

cpp 复制代码
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->comboBox->addItem("麦辣鸡腿堡");
    ui->comboBox->addItem("巨无霸");
    ui->comboBox->addItem("培根蔬萃双层牛堡");

    ui->comboBox_2->addItem("薯条");
    ui->comboBox_2->addItem("麦乐鸡块");
    ui->comboBox_2->addItem("麦辣鸡翅");

    ui->comboBox_3->addItem("可乐");
    ui->comboBox_3->addItem("雪碧");
}

void Widget::on_pushButton_clicked()
{
    qDebug() << ui->comboBox->currentText() << ", " << ui->comboBox_2->currentText() << ", " << ui->comboBox_3->currentText();
}

右击下拉框,编辑项目也可以添加内容。

另一个例子,通过文件/网络来添加下拉框内容

cpp 复制代码
#include <QDebug>
#include <fstream>

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

    std::ifstream file("E:/test.txt");
    if (!file.is_open())
    {
        qDebug() << "文件打开失败";
        return ;
    }
    std::string line;
    while (std::getline(file, line))
    {
        // 括号中把std::string 改为 QString
        // QString 改为 std::string 是 .toStdString()
        ui->comboBox->addItem(QString::fromStdString(line));
    }
    file.close();
}

2、QSpinBox

带有按钮的输入框,微调框。



在这里插入图片描述](https://i-blog.csdnimg.cn/direct/5c9560d549594a12b801829e81986137.png)

cpp 复制代码
#include <QDebug>

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

    ui->comboBox->addItem("麦辣鸡腿堡");
    ui->comboBox->addItem("巨无霸");
    ui->comboBox->addItem("培根蔬萃双层牛堡");

    ui->comboBox_2->addItem("薯条");
    ui->comboBox_2->addItem("麦乐鸡块");
    ui->comboBox_2->addItem("麦辣鸡翅");

    ui->comboBox_3->addItem("可乐");
    ui->comboBox_3->addItem("雪碧");

    // 限制一下可选的量
    ui->spinBox->setRange(1, 5);
    ui->spinBox_2->setRange(1, 5);
    ui->spinBox_3->setRange(1, 5);

    ui->spinBox->setValue(1);
    ui->spinBox_2->setValue(1);
    ui->spinBox_3->setValue(1);
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "当前订单: "
             << ui->comboBox->currentText() << ": " << ui->spinBox->value()
             << ui->comboBox_2->currentText() << ": " << ui->spinBox_2->value()
             << ui->comboBox_3->currentText() << ": " << ui->spinBox_3->value();
}

3、QDateTimeEdit


写一个时间计算器

cpp 复制代码
void Widget::on_pushButton_clicked()
{
    QDateTime timeOld = ui->dateTimeEdit->dateTime();
    QDateTime timeNew = ui->dateTimeEdit_2->dateTime();

    // QDateTime中, daysTo计算两个时间的日期的差值, secsTo计算两个时间的秒数的差值
    int days = timeOld.daysTo(timeNew);
    int seconds = timeOld.secsTo(timeNew);
    
    int hours = (seconds / 3600) % 24; // 秒转为小时
    ui->label->setText(QString("相差 ") + QString::number(days) + QString(" 天 ") + QString::number(hours) + QString(" 小时"));
}

也可以自己计算天数

cpp 复制代码
void Widget::on_pushButton_clicked()
{
    QDateTime timeO ld = ui->dateTimeEdit->dateTime();
    QDateTime timeNew = ui->dateTimeEdit_2->dateTime();

    // QDateTime中, daysTo计算两个时间的日期的差值, secsTo计算两个时间的秒数的差值
    //int days = timeOld.daysTo(timeNew);
    int seconds = timeOld.secsTo(timeNew);
    
    int days = (seconds / 3600) / 24;
    int hours = (seconds / 3600) % 24; // 秒转为小时
    ui->label->setText(QString("相差 ") + QString::number(days) + QString(" 天 ") + QString::number(hours) + QString(" 小时"));
}

4、QDial

表示旋钮




通过旋钮控制窗口的不透明度

cpp 复制代码
void Widget::on_dial_valueChanged(int value)
{
    // 1完全不透明, 0完全透明, 是一个小数
    this->setWindowOpacity((double)value / 100);
}

5、QSlider

滑动条


信号

写一个水平,一个垂直进度条,用方向键可以控制。

cpp 复制代码
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->horizontalSlider->setMinimum(100);
    ui->horizontalSlider->setMaximum(2100);
    ui->horizontalSlider->setValue(400);
    ui->horizontalSlider->setSingleStep(50);

    ui->verticalSlider->setMinimum(100);
    ui->verticalSlider->setMaximum(2100);
    ui->verticalSlider->setValue(700);
    ui->verticalSlider->setSingleStep(50);
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value, rect.height());
}

void Widget::on_verticalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), rect.width(), value);
}

用快捷键来操作滑动条

cpp 复制代码
# include <QShortCut>

    QShortcut* shortCut1 = new QShortcut(this);
    shortCut1->setKey(QKeySequence("-"));
    QShortcut* shortCut2 = new QShortcut(this);
    shortCut2->setKey(QKeySequence("+"));

    connect(shortCut1, &QShortcut::activated, this, &Widget::subValue);
    connect(shortCut2, &QShortcut::activated, this, &Widget::addValue);

void Widget::subValue()
{
    int value = ui->horizontalSlider->value();
    if (value <= ui->horizontalSlider->minimum())
        return;
    ui->horizontalSlider->setValue(value - 5);
}

void Widget::addValue()
{
    int value = ui->horizontalSlider->value();
    if (value >= ui->horizontalSlider->maximum())
        return;
    ui->horizontalSlider->setValue(value + 5);
}

结束。

相关推荐
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054965 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月5 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
m0_531237175 天前
C语言-数组练习进阶
c语言·开发语言·算法