QT系列第5节 QT中常用输入控件

Source

QT中经常利用控件来获取用户输入数据,本篇将介绍常用的用户输入控件

目录

(1) QSpinBox

(2) QDoubleSpinBox

(3)QSlider

(4) QScrollBar

(5)QProgressBar

(6)QDial

(7)QLCDNumer

(8)QDateEdit

(9)QTimeEdit

(10)QDateTimeEdit

(11)QTimer

(12)测试程序


(1) QSpinBox

带其他信息的整数增减输入框

(2) QDoubleSpinBox

带其他信息的浮点数增加输入框

(3)QSlider

滑动条

(4) QScrollBar

卷滚条

(5)QProgressBar

进度条

(6)QDial

表盘样式数值输入组件

(7)QLCDNumer

类似LCD展示面板

(8)QDateEdit

日期控件

(9)QTimeEdit

时间控件

(10)QDateTimeEdit

日期时间控件

(11)QTimer

定时器

(12)测试程序

1.运行效果

  

2.关键代码

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

private:
    QTimer *fTimer;
    QTimer *fTimerCounter;

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

private slots:
    void on_pushButton_clicked();

    void on_horizontalScrollBar_valueChanged(int value);

    void on_horizontalSliderRed_valueChanged(int value);

    void on_horizontalSliderGreen_valueChanged(int value);

    void on_horizontalSliderlue_valueChanged(int value);

    void on_horizontalSliderlueAlpha_valueChanged(int value);

    void on_dial_valueChanged(int value);

    void on_radioButtonDec_clicked();

    void on_radioButtonBin_clicked();

    void on_radioButtonOct_clicked();

    void on_radioButtonHex_clicked();

    void on_pushButton_2_clicked();

    void on_timer_timeout();
    void on_pushButtonStartTimer_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    fTimer = new QTimer(this);
    fTimer->setInterval(1000);
    fTimer->stop();
    connect(fTimer, SIGNAL(timeout()), this, SLOT(on_timer_timeout()));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    int num = ui->spinBox->value();
    float fdata = ui->doubleSpinBox->value();
    qDebug() << num << " " << fdata;

    int value = ui->spinBoxWeight->value();
    ui->spinBoxBin->setValue(value);
    ui->spinBoxDec->setValue(value);
    ui->spinBoxHex->setValue(value);
}


void Widget::on_horizontalScrollBar_valueChanged(int value)
{
    Q_UNUSED(value)
}


void Widget::on_horizontalSliderRed_valueChanged(int value)
{
    Q_UNUSED(value)
    QPalette palette = ui->textEdit->palette();
    QColor color;
    color.setRgb(ui->horizontalSliderRed->value(),
                 ui->horizontalSliderGreen->value(),
                 ui->horizontalSliderBlue->value(),
                 ui->horizontalSliderAlpha->value());
    palette.setColor(QPalette::Base, color);
ui->textEdit->setPalette(palette);
}


void Widget::on_horizontalSliderGreen_valueChanged(int value)
{
    on_horizontalSliderRed_valueChanged(value);
}


void Widget::on_horizontalSliderlue_valueChanged(int value)
{
    on_horizontalSliderRed_valueChanged(value);
}


void Widget::on_horizontalSliderlueAlpha_valueChanged(int value)
{
    on_horizontalSliderRed_valueChanged(value);
}


void Widget::on_dial_valueChanged(int value)
{
    ui->lcdNumber->display(value);
}


void Widget::on_radioButtonDec_clicked()
{
    ui->lcdNumber->setDigitCount(3);
    ui->lcdNumber->setDecMode();
}


void Widget::on_radioButtonBin_clicked()
{
    ui->lcdNumber->setDigitCount(3);
    ui->lcdNumber->setBinMode();
}


void Widget::on_radioButtonOct_clicked()
{
    ui->lcdNumber->setDigitCount(3);
    ui->lcdNumber->setOctMode();
}


void Widget::on_radioButtonHex_clicked()
{
    ui->lcdNumber->setDigitCount(3);
    ui->lcdNumber->setHexMode();
}


void Widget::on_pushButton_2_clicked()
{
    QDateTime curTime = QDateTime::currentDateTime();
    ui->dateTimeEdit->setDateTime(curTime);
    ui->dateEdit->setDate(curTime.date());
    ui->timeEdit->setTime(curTime.time());

    ui->timeEditSelf->setText(curTime.toString("hh:mm:ss"));
    ui->dateEditSelf->setText(curTime.toString("yyyy-MM-dd"));
    ui->lineEditDateTime->setText(curTime.toString("yyyy-MM-dd hh:mm:ss"));

    QString str = curTime.toString("hh:mm:ss");
    QTime times = QTime::fromString(str, "hh:mm:ss");
    ui->timeEdit_2->setTime(times);
    qDebug() << times.toString("hh时mm分ss秒");

    str = curTime.toString("yyyy-MM-dd");
    QDate date = QDate::fromString(str, "yyyy-MM-dd");
    ui->dateEdit_2->setDate(date);
    qDebug() << date.toString("yyyy年M月d日");


    str = curTime.toString("yyyy-MM-dd hh:mm:ss");
    QDateTime dt = QDateTime::fromString(str, "yyyy-MM-dd hh:mm:ss");
    ui->dateTimeEdit_2->setDateTime(dt);
}

void Widget::on_timer_timeout() {
    ui->lcdNumberHour->display(QTime::currentTime().hour());
    ui->lcdNumberMinute->display(QTime::currentTime().minute());
    ui->lcdNumberSecond->display(QTime::currentTime().second());
}


void Widget::on_pushButtonStartTimer_clicked()
{
    fTimer->start();
}

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>766</width>
    <height>308</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QSpinBox" name="spinBox">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>161</width>
     <height>22</height>
    </rect>
   </property>
   <property name="value">
    <number>10</number>
   </property>
  </widget>
  <widget class="QDoubleSpinBox" name="doubleSpinBox">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>20</y>
     <width>161</width>
     <height>22</height>
    </rect>
   </property>
   <property name="value">
    <double>1.230000000000000</double>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>170</y>
     <width>81</width>
     <height>24</height>
    </rect>
   </property>
   <property name="text">
    <string>控制spinbox</string>
   </property>
  </widget>
  <widget class="QSpinBox" name="spinBoxDec">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>80</y>
     <width>111</width>
     <height>22</height>
    </rect>
   </property>
   <property name="prefix">
    <string>Dec  </string>
   </property>
   <property name="value">
    <number>12</number>
   </property>
  </widget>
  <widget class="QSpinBox" name="spinBoxBin">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>110</y>
     <width>111</width>
     <height>22</height>
    </rect>
   </property>
   <property name="prefix">
    <string>Bin  </string>
   </property>
   <property name="displayIntegerBase">
    <number>2</number>
   </property>
  </widget>
  <widget class="QSpinBox" name="spinBoxHex">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>140</y>
     <width>111</width>
     <height>22</height>
    </rect>
   </property>
   <property name="prefix">
    <string>Hex  </string>
   </property>
   <property name="displayIntegerBase">
    <number>16</number>
   </property>
  </widget>
  <widget class="QSpinBox" name="spinBoxWeight">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>50</y>
     <width>111</width>
     <height>22</height>
    </rect>
   </property>
   <property name="suffix">
    <string>  KG</string>
   </property>
   <property name="value">
    <number>10</number>
   </property>
  </widget>
  <widget class="QScrollBar" name="horizontalScrollBar">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>10</y>
     <width>160</width>
     <height>16</height>
    </rect>
   </property>
   <property name="value">
    <number>10</number>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="QScrollBar" name="verticalScrollBar">
   <property name="geometry">
    <rect>
     <x>290</x>
     <y>130</y>
     <width>16</width>
     <height>160</height>
    </rect>
   </property>
   <property name="pageStep">
    <number>1</number>
   </property>
   <property name="value">
    <number>20</number>
   </property>
   <property name="orientation">
    <enum>Qt::Vertical</enum>
   </property>
   <property name="invertedAppearance">
    <bool>false</bool>
   </property>
  </widget>
  <widget class="QSlider" name="horizontalSliderRed">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>30</y>
     <width>160</width>
     <height>22</height>
    </rect>
   </property>
   <property name="maximum">
    <number>255</number>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="QSlider" name="verticalSlider">
   <property name="geometry">
    <rect>
     <x>320</x>
     <y>130</y>
     <width>22</width>
     <height>160</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Vertical</enum>
   </property>
  </widget>
  <widget class="QDial" name="dial">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>240</y>
     <width>51</width>
     <height>61</height>
    </rect>
   </property>
  </widget>
  <widget class="QLCDNumber" name="lcdNumber">
   <property name="geometry">
    <rect>
     <x>370</x>
     <y>140</y>
     <width>64</width>
     <height>71</height>
    </rect>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButtonDec">
   <property name="geometry">
    <rect>
     <x>360</x>
     <y>220</y>
     <width>95</width>
     <height>20</height>
    </rect>
   </property>
   <property name="text">
    <string>十进制</string>
   </property>
   <property name="checked">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButtonBin">
   <property name="geometry">
    <rect>
     <x>360</x>
     <y>240</y>
     <width>95</width>
     <height>20</height>
    </rect>
   </property>
   <property name="text">
    <string>二进制</string>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButtonOct">
   <property name="geometry">
    <rect>
     <x>360</x>
     <y>260</y>
     <width>95</width>
     <height>20</height>
    </rect>
   </property>
   <property name="text">
    <string>八进制</string>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButtonHex">
   <property name="geometry">
    <rect>
     <x>360</x>
     <y>280</y>
     <width>95</width>
     <height>20</height>
    </rect>
   </property>
   <property name="text">
    <string>十六进制</string>
   </property>
  </widget>
  <widget class="QSlider" name="horizontalSliderGreen">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>60</y>
     <width>160</width>
     <height>22</height>
    </rect>
   </property>
   <property name="maximum">
    <number>255</number>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="QSlider" name="horizontalSliderBlue">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>90</y>
     <width>160</width>
     <height>22</height>
    </rect>
   </property>
   <property name="maximum">
    <number>255</number>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="QSlider" name="horizontalSliderAlpha">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>120</y>
     <width>160</width>
     <height>22</height>
    </rect>
   </property>
   <property name="maximum">
    <number>255</number>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="QTextEdit" name="textEdit">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>150</y>
     <width>104</width>
     <height>71</height>
    </rect>
   </property>
   <property name="palette">
    <palette>
     <active>
      <colorrole role="Base">
       <brush brushstyle="SolidPattern">
        <color alpha="255">
         <red>85</red>
         <green>255</green>
         <blue>255</blue>
        </color>
       </brush>
      </colorrole>
     </active>
     <inactive>
      <colorrole role="Base">
       <brush brushstyle="SolidPattern">
        <color alpha="255">
         <red>255</red>
         <green>255</green>
         <blue>255</blue>
        </color>
       </brush>
      </colorrole>
     </inactive>
     <disabled>
      <colorrole role="Base">
       <brush brushstyle="SolidPattern">
        <color alpha="255">
         <red>240</red>
         <green>240</green>
         <blue>240</blue>
        </color>
       </brush>
      </colorrole>
     </disabled>
    </palette>
   </property>
  </widget>
  <widget class="QDateEdit" name="dateEdit">
   <property name="geometry">
    <rect>
     <x>390</x>
     <y>10</y>
     <width>110</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QTimeEdit" name="timeEdit">
   <property name="geometry">
    <rect>
     <x>390</x>
     <y>40</y>
     <width>118</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QDateTimeEdit" name="dateTimeEdit">
   <property name="geometry">
    <rect>
     <x>380</x>
     <y>80</y>
     <width>194</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_2">
   <property name="geometry">
    <rect>
     <x>680</x>
     <y>130</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="text">
    <string>当前时间</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="timeEditSelf">
   <property name="geometry">
    <rect>
     <x>560</x>
     <y>10</y>
     <width>113</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="dateEditSelf">
   <property name="geometry">
    <rect>
     <x>560</x>
     <y>40</y>
     <width>113</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEditDateTime">
   <property name="geometry">
    <rect>
     <x>590</x>
     <y>80</y>
     <width>171</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QTimeEdit" name="timeEdit_2">
   <property name="geometry">
    <rect>
     <x>450</x>
     <y>120</y>
     <width>118</width>
     <height>22</height>
    </rect>
   </property>
   <property name="displayFormat">
    <string>HH:mm:ss</string>
   </property>
  </widget>
  <widget class="QDateEdit" name="dateEdit_2">
   <property name="geometry">
    <rect>
     <x>450</x>
     <y>150</y>
     <width>110</width>
     <height>22</height>
    </rect>
   </property>
   <property name="displayFormat">
    <string>yyyy/MM/dd</string>
   </property>
  </widget>
  <widget class="QDateTimeEdit" name="dateTimeEdit_2">
   <property name="geometry">
    <rect>
     <x>450</x>
     <y>180</y>
     <width>194</width>
     <height>22</height>
    </rect>
   </property>
   <property name="displayFormat">
    <string>yyyy/MM/dd HH:mm:ss</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButtonStartTimer">
   <property name="geometry">
    <rect>
     <x>690</x>
     <y>210</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="text">
    <string>启动定时器</string>
   </property>
  </widget>
  <widget class="QLCDNumber" name="lcdNumberHour">
   <property name="geometry">
    <rect>
     <x>450</x>
     <y>210</y>
     <width>64</width>
     <height>23</height>
    </rect>
   </property>
   <property name="segmentStyle">
    <enum>QLCDNumber::Flat</enum>
   </property>
  </widget>
  <widget class="QLCDNumber" name="lcdNumberMinute">
   <property name="geometry">
    <rect>
     <x>520</x>
     <y>210</y>
     <width>64</width>
     <height>23</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::StyledPanel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Sunken</enum>
   </property>
   <property name="segmentStyle">
    <enum>QLCDNumber::Flat</enum>
   </property>
  </widget>
  <widget class="QLCDNumber" name="lcdNumberSecond">
   <property name="geometry">
    <rect>
     <x>590</x>
     <y>210</y>
     <width>64</width>
     <height>23</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::Box</enum>
   </property>
   <property name="segmentStyle">
    <enum>QLCDNumber::Flat</enum>
   </property>
  </widget>
  <zorder>pushButtonStartTimer</zorder>
  <zorder>spinBox</zorder>
  <zorder>doubleSpinBox</zorder>
  <zorder>pushButton</zorder>
  <zorder>spinBoxDec</zorder>
  <zorder>spinBoxBin</zorder>
  <zorder>spinBoxHex</zorder>
  <zorder>spinBoxWeight</zorder>
  <zorder>horizontalScrollBar</zorder>
  <zorder>verticalScrollBar</zorder>
  <zorder>horizontalSliderRed</zorder>
  <zorder>verticalSlider</zorder>
  <zorder>dial</zorder>
  <zorder>lcdNumber</zorder>
  <zorder>radioButtonDec</zorder>
  <zorder>radioButtonBin</zorder>
  <zorder>radioButtonOct</zorder>
  <zorder>radioButtonHex</zorder>
  <zorder>horizontalSliderGreen</zorder>
  <zorder>horizontalSliderBlue</zorder>
  <zorder>horizontalSliderAlpha</zorder>
  <zorder>textEdit</zorder>
  <zorder>dateEdit</zorder>
  <zorder>timeEdit</zorder>
  <zorder>dateTimeEdit</zorder>
  <zorder>pushButton_2</zorder>
  <zorder>timeEditSelf</zorder>
  <zorder>dateEditSelf</zorder>
  <zorder>lineEditDateTime</zorder>
  <zorder>timeEdit_2</zorder>
  <zorder>dateEdit_2</zorder>
  <zorder>dateTimeEdit_2</zorder>
  <zorder>lcdNumberHour</zorder>
  <zorder>lcdNumberMinute</zorder>
  <zorder>lcdNumberSecond</zorder>
 </widget>
 <resources/>
 <connections/>
</ui>