0
点赞
收藏
分享

微信扫一扫

Qt 控件之输入窗口部件

孟祥忠诗歌 2022-03-11 阅读 102

Qt 控件之输入窗口部件

Qt Designer 窗口部件提供的面板中,提供了 16 种输入部件:

下面将通过例子讲解每种输入窗口部件是如何使用,并能实现什么效果

1. QComboBox

QComboBox 类提供了 Qt 下拉组合框的组件。

以下实例实现的效果:通过点击下拉按钮的项,选择其中一项,然后打印出当前选择项的内容

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
/* 引入QComboBox */
#include <QComboBox>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    /* 声明一个QComboBox对象 */
    QComboBox *comboBox;
private slots:
    /* 声明QComboBox对象的槽函数 */
     void comboBoxIndexChanged(int);
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"
/* 引入QDebug */
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    /* 设置主窗体的显示位置与大小 */
    this->setGeometry(0, 0, 800, 480);
    /* 实例化对象 */
    comboBox = new QComboBox(this);
    /* 设置comboBox的显示位置与大小 */
    comboBox->setGeometry(300, 200, 150, 30);
    /* 添加项,我们添加三个省份,作为comboBox的三个选项 */
    comboBox->addItem("广东(默认)");
    comboBox->addItem("湖南");
    comboBox->addItem("四川");
    /* 信号槽连接 */
    connect(comboBox, SIGNAL(currentIndexChanged(int)), this,
            SLOT(comboBoxIndexChanged(int)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::comboBoxIndexChanged(int index)
{
    /* 打印出选择的省份 */
    qDebug()<<"您选择的省份是"<< comboBox->itemText(index)<<endl;
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,当点击下拉选择框选择省份时,槽函数将打印出您选择的省份。

2. QFontComboBox

QFontComboBox 类提供了下拉选择字体系列的组合框小部件。

以下实例实现的效果:字体选择,通过点击下拉按钮的项,选择其中一项然后打印出当前选择项的内容

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
/* 引入QFontComboBox */
#include <QFontComboBox>
/* 引入QLable */
#include <QLabel>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    /* 声明一个QFontComboBox对象 */
    QFontComboBox *fontComboBox;
    /* 声明一个Label对象,用于显示当前字体变化 */
    QLabel *label;
private slots:
    /* 声明QFontComboBox对象使用的槽函数 */
    void fontComboBoxFontChanged(QFont);
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 设置主窗体的显示位置和大小 */
    this->setGeometry(0, 0, 800, 480);
    /* 实例化对象 */
    fontComboBox = new QFontComboBox(this);
    label = new QLabel(this);
    /* 设置显示的位置与大小 */
    fontComboBox->setGeometry(280, 200, 200, 30);
    label->setGeometry(280, 250, 300, 50);
    /* 信号与槽连接 */
    connect(fontComboBox, SIGNAL(currentFontChanged(QFont)), this,
            SLOT(fontComboBoxFontChanged(QFont)));
}

MainWindow::~MainWindow()
{
}

/* 槽函数实现 */
void MainWindow::fontComboBoxFontChanged(QFont font)
{
    /* 将label里的文本内容设置为所选择的字体 */
    label->setFont(font);
    /* 定义一个字符串接收当前项的字体 */
    QString str = "用此标签显示字体效果\n设置的字体为:" +
            fontComboBox->itemText(fontComboBox->currentIndex());
    /* 将字符串的内容作为label的显示内容 */
    label->setText(str);
}

  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,当点击 FontCOmboBox 字体组合框选择字体后, Label 标签显示的字体将改变为当前所选择的字体

3. QTextEdit

QLineEdit 小部件是一个单行文本编辑器。

以下实例实现的效果:单行输入框 ,通过点击下拉按钮的项,选择其中一项,然
后打印出当前选择项的内容

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    /* 声明一个QLineEdit对象 */
    QLineEdit *lineEdit;
    /* 声明一个QPushButton对象 */
    QPushButton *pushButton;
    /* 声明一个QLabel对象 */
    QLabel *label;
private slots:
    /* 声明一个槽函数,响应pushButton的clicked事件 */
    void pushButtonClicked();
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 800, 480);

    lineEdit = new QLineEdit(this);
    lineEdit->setGeometry(280, 200, 200, 20);

    pushButton = new QPushButton(this);
    pushButton->setGeometry(500, 200, 50, 20);
    pushButton->setText("确认");

    label = new QLabel(this);
    label->setGeometry(280, 250, 400, 20);
    label->setText("你输入的内容是:");

    /* 信号槽连接 */
    connect(pushButton,SIGNAL(clicked()), this,
            SLOT(pushButtonClicked()));
}

MainWindow::~MainWindow()
{
}

void MainWindow::pushButtonClicked()
{
    /* 字符串变量str */
    QString str;

    str = "你输入的内容是:";
    str += lineEdit->text();

    /* 设置label里的文本显示内容 */
    label->setText(str);
    /* 在点击了确认键之后清空lineEdit单行输入框 */
    lineEdit->clear();
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,当在 QLineEdit 单行输入框内输入文本内容后,单击 QPushButton 确认按钮后, QLabel 的文本内容将显示所输入的内容

4. QTextEdit

QTextEdit 类提供了一个查看器 编辑器小部件。

以下实例实现的效果:演示文本的输入

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QTextEdit>
#include <QPushButton>

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    /* 声明一个QTextEdit对象 */
    QTextEdit *textEdit;
    /* 声明两个QPushButton对象 */
    QPushButton *pushButtonSelectAll;
    QPushButton *pushButtonClearAll;
private slots:
    /* 声明两个槽函数,响应按钮点击响应的事件 */
    void pushButtonSelectAllClicked();
    void pushButtonClearAllClicked();
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下


  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,在编辑框里输入文字后,点击按钮全选,点击清除则清除编辑框内的全部内容

5. QPlainTextEdit

QPlainTextEdit 类提供了一个用于编辑和显示纯文本的小部件,常用于显示多行文本或简单文本。

以下实例实现的效果:用一个 QPlainTextEdit 来读取本当前工程里的一个文件,并用一个 RadioButton 里将 QPlainTextEdit 设为只读

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QPlainTextEdit>
#include <QRadioButton>

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    /* 声明对象 */
    QPlainTextEdit *plainTextEdit;
    QRadioButton *radioButton;
private slots:
    /* 槽函数 */
    void radioButtonClicked();
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"
#include <QDir>
#include <QTextStream>
#include <QCoreApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 设置当前程序的工作目录为可执行程序的工作目录 */
    QDir::setCurrent(QCoreApplication::applicationDirPath());

    this->setGeometry(0, 0, 800, 480);

    plainTextEdit = new QPlainTextEdit(this);
    plainTextEdit->setGeometry(0, 50, 800, 430);

    radioButton = new QRadioButton(this);
    radioButton->setGeometry(650, 20, 100, 20);
    radioButton->setText("只读模式");

    /* 打开可执行程序目录里的moc_mainwindow.cpp,注意如果是Windows下
   moc_mainwindow.cpp并不在当前目录,而在上一级目录"../moc_mainwindow.cpp"*/
    QFile file("moc_mainwindow.cpp");

    /* 以只读模式打开,但是可以在plainTextEdit里编辑 */
    file.open((QFile::ReadOnly | QFile::Text));

    /* 加载到文件流 */
    QTextStream in(&file);

    /* 从文本流中读取全部 */
    plainTextEdit->insertPlainText(in.readAll());

    /* 信号槽连接 */
    connect(radioButton, SIGNAL(clicked()), this,
            SLOT(radioButtonClicked()));

}

MainWindow::~MainWindow()
{
}

void MainWindow::radioButtonClicked()
{
    /* 检查radioButton是否选中 */
    if(radioButton->isChecked()) {
        /* 设置为只读模式 */
        plainTextEdit->setReadOnly(true);
    } else {
        /* 设置为非只读模式 */
        plainTextEdit->setReadOnly(false);
    }
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,在 QPlainTextEdit 编辑框下是可编辑的,当选中程序界面上的只读模式时, QPlainTextEdit 编辑框就不可以再编辑,相反也可取消只读模式再编辑

6. QSpinBox

QSpinBox 类提供了一个微调框小部件。

以下实例实现的效果:用一个 QSpinBox 来调节程序窗体的整体不透明度。

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSpinBox>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    /* 声明一个QSpinBox对象 */
    QSpinBox *spinBox;
private slots:
    /* 槽函数 */
    void spinBoxValueChanged(int);
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 800, 480);

    /* 设置主窗口背景颜色,rgb颜色标准,a代表不透明度(0~100)*/
    this->setStyleSheet("QMainWindow{background-color: "
                        "rgba(100, 100, 100, 100%) }");

    spinBox = new QSpinBox(this);
    spinBox->setGeometry(350, 200, 150, 30);

    /* 设置范围0~100 */
    spinBox->setRange(0, 100);

    /* 设置步长为10 */
    spinBox->setSingleStep(10);

    /* 设置初始值为100 */
    spinBox->setValue(100);

    /* 设置后缀 */
    spinBox->setSuffix("%不透明度");

    /* 信号槽连接 */
    connect(spinBox,SIGNAL(valueChanged(int)), this,
            SLOT(spinBoxValueChanged(int)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::spinBoxValueChanged(int opacity)
{
    /* 转换为double数据类型 */
    double  dobleopacity = (double)opacity / 100;

    /* 设置窗体不透明度,范围是0.0~1.0。1则为不透明,0为全透明 */
    this->setWindowOpacity(dobleopacity);
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,程序初始化界面时是全不透明,不透明度值为 100%,当不透明度的值变小时,窗口将透明化

7. QDoubleSpinBox

QDoubleSpinBox 类提供了一个用于处理浮点值微调框小部件。与 QSpinBox 作用基本一样,与 QSpinBox 不同的是, QDoubleSpinBox 类处理的是浮点值数据

以下实例实现的效果:用一个 QDoubleSpinBox 来调节程序窗口的整体大小

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDoubleSpinBox>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    /* 声明一个QDoubleSpinBox对象 */
    QDoubleSpinBox *doubleSpinBox;

private slots:
    /* 槽函数 */
    void doubleSpinBoxValueChanged(double);

};
#endif // MAINWINDOW_H

  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 800, 480);

    /* 实例化和设置显示的位置与大小 */
    doubleSpinBox = new QDoubleSpinBox(this);
    doubleSpinBox->setGeometry((this->width() - 200) / 2,
                               (this->height() - 30) / 2,
                               200, 30);
    /* 设置前缀 */
    doubleSpinBox->setPrefix("窗口大小");

    /* 设置后缀 */
    doubleSpinBox->setSuffix("%");

    /* 设置范围 */
    doubleSpinBox->setRange(50.00, 100.00);

    /* 设置初始值 */
    doubleSpinBox->setValue(100.00);

    /* 设置步长 */
    doubleSpinBox->setSingleStep(0.1);

    /* 信号槽连接 */
    connect(doubleSpinBox, SIGNAL(valueChanged(double)),
            SLOT(doubleSpinBoxValueChanged(double)));

}

MainWindow::~MainWindow()
{
}

void MainWindow::doubleSpinBoxValueChanged(double value)
{
    /* 重新计算窗口的宽与高 */
    int w = 800 * value / 100;
    int h = 480 * value / 100;

    /* 重新设置窗口的宽与高 */
    this->setGeometry(0, 0, w, h);

    /* 重新设置doubleSpinBox的显示位置 */
    doubleSpinBox->setGeometry((this->width() - 200) / 2,
                               (this->height() - 30) / 2,
                               200, 30);
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,程序初始化界面窗口大小值为 100%,当点击向下调节 QDoubleSpinBox 时,整个窗体将按 QDoubleSpinBox 里数值的比例缩小,相反当点击向上调节 QDoubleSpinBox 时,窗口大小将整体变大

8. QTimeEdit

QTimeEdit 类提供一个基于 QDateTimeEdit 类编辑时间的小部件。实例见第10小节

9. QDateEdit

QDateEdit 类提供一个基于 QDateTimeEdit 类编辑时间的小部件。实例见第10小节

10. QDateTimeEdit

QDateTimeEdit 类提供了一个用于编辑日期和时间的小部件,允许用户使用键盘或箭头键编辑日期,以增加或减少日期和时间值

以下实例实现的效果:使用一个 QDateTimeEdit ,一个 QTimeEdit,以及一个 QDateEdit ,传入当前系统时间与日期,展示简单的日期时间控件使用方法

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDateTimeEdit>
#include <QTimeEdit>
#include <QDateEdit>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    /* 声明对象 */
    QDateTimeEdit *dateTimeEdit;
    QTimeEdit *timeEdit;
    QDateEdit *dateEdit;
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 设置位置与大小 */
    this->setGeometry(0, 0, 800, 480);

    /*实例化对象,传入当前日期与时间*/
    dateTimeEdit = new QDateTimeEdit(
                QDateTime::currentDateTime(),this);
    dateTimeEdit->setGeometry(300, 200, 200, 30);
    /* 弹出日期控件与否 */
    //dateTimeEdit->setCalendarPopup(true);

    /* 实例化对象,传入当前时间 */
    timeEdit = new QTimeEdit(QTime::currentTime(),this);
    timeEdit->setGeometry(300, 240, 200, 30);

    /* 实例化对象,传入当前日期 */
    dateEdit = new QDateEdit(QDate::currentDate(),this);
    dateEdit->setGeometry(300, 280, 200, 30);
}

MainWindow::~MainWindow()
{
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,分别显示系统当前的时间与日期

11. QDial

QDial类提供了一个圆形范围控制。

以下实例实现的效果:车速表,使用一个 QDial ,以一个 QLabel ,演示 QDial 的用法,当程序初始化界面后,拖动滑块的位置, label 则会显示 dial 的值

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDial>
#include <QLabel>
#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    /* 声明对象 */
    QDial *dial;
    QLabel *label;

private slots:
    /* 槽函数 */
    void dialValueChanged(int);

};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 设置主窗体的位置与大小 */
    this->setGeometry(0, 0, 800, 480);

    /* 实例化对象和设置显示位置与大小 */
    dial = new QDial(this);
    dial->setGeometry(300, 100, 200, 200);

    /* 设置页长(两个最大刻度的间距)*/
    dial->setPageStep(10);

    /* 设置刻度可见 */
    dial->setNotchesVisible(true);

    /* 设置两个凹槽之间的目标像素数 */
    dial->setNotchTarget(1.00);

    /* 设置dial值的范围 */
    dial->setRange(0,100);

    /* 开启后可指向圆的任何角度 */
    //dial->setWrapping(true);

    /* 实例化对象和设置显示位置与大小 */
    label = new QLabel(this);
    label->setGeometry(370, 300, 200, 50);

    /* 初始化为0km/h */
    label->setText("0km/h");

    /* 信号槽连接 */
    connect(dial, SIGNAL(valueChanged(int)),
            this, SLOT(dialValueChanged(int)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::dialValueChanged(int val)
{
    /* QString::number()转换成字符串 */
    label->setText(QString::number(val) + "km/h");
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,当用鼠标拖动滑块或者按键盘的上下左右方向键时, label 则会显示当前“车速”

12. QScrollBar

QScrollBar 继承 QAbstractSlider ,提供垂直或水平滚动条,提供了用户在文档中的当前位置和可见文档数量的可视化指示。滚动条通常配有其他控件,可以实现更精确的导航 这里指浏览到精确的位置

以下实例实现的效果:创建水平和垂直滚动条(难度:简单)

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QScrollBar>
#include <QLabel>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    /* 声明水平滚动条对象 */
    QScrollBar *horizontalScrollBar;

    /* 声明垂直滚动条对象 */
    QScrollBar *verticalScrollBar;

    /* 声明标签文本 */
    QLabel *label;
};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 设置主窗体大小,位置 */
    this->setGeometry(0,0,800,480);

    /* 实例化水平滚动条及设置位置大小 */
    horizontalScrollBar = new QScrollBar(Qt::Horizontal, this);
    horizontalScrollBar->setGeometry(0, 450, 800, 30);

    /* 实例化垂直滚动条及设置位置大小 */
    verticalScrollBar = new QScrollBar(Qt::Vertical, this);
    verticalScrollBar->setGeometry(770, 0, 30, 480);

    /* 实例化,标签文本 */
    label = new QLabel(this);
    /* 设置文本 */
    label->setText("这是一个测试");
    /* 设置位置大小 */
    label->setGeometry(300, 200, 100, 20);
}

MainWindow::~MainWindow()
{
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,会显示水平和垂直滚动条

13. QSlider

QSlider 继承 QAbstractSlider ,提供垂直或水平滑动条小部件,滑动条是用于控制有界值的典型小部件。允许用户沿着水平或垂直凹槽移动滑块手柄,并将手柄的位置转换为合法范围内的整数值

以下实例实现的效果:创建水平和垂直滑动条

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSlider>
#include <QLabel>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    /* 声明对象 */
    QSlider *horizontalSlider;
    QSlider *verticalSlider;
    QLabel *label;
private slots:
    /* 槽函数 */
    void horizontalSliderValueChanged(int);
    void verticalSliderValueChanged(int);

};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 800, 480);

    /* 实例化水平滑动条对象*/
    horizontalSlider = new QSlider(Qt::Horizontal, this);

    /* 设置显示的位置与大小 */
    horizontalSlider->setGeometry(250, 100, 200, 20);

    /* 设置值范围 */
    horizontalSlider->setRange(0, 100);

    /* 实例化垂直滑动条对象 */
    verticalSlider = new QSlider(Qt::Vertical, this);

    /* 设置显示的位置与大小 */
    verticalSlider->setGeometry(200, 50, 20, 200);

    /* 设置值范围 */
    verticalSlider->setRange(0, 100);

    /* 实例化标签文本 */
    label = new QLabel("滑动条值:0", this);
    label->setGeometry(250, 200, 100, 20);

    /* 信号槽连接 */
    connect(horizontalSlider, SIGNAL(valueChanged(int)),
            this, SLOT(horizontalSliderValueChanged(int)));
    connect(verticalSlider, SIGNAL(valueChanged(int)),
            this, SLOT(verticalSliderValueChanged(int)));

}

MainWindow::~MainWindow()
{
}

void MainWindow::horizontalSliderValueChanged(int val)
{
    /* 当水平滑动条的值改变时,改变垂直滑动条的值 */
    verticalSlider->setSliderPosition(val);

    /* 将int类型转变成字符 */

    QString str = "滑动条值:" + QString::number(val);

    /* 显示当前垂直或水平滑动条的值 */
    label->setText(str);

}

void MainWindow::verticalSliderValueChanged(int val)
{
    /* 当垂直滑动条的值改变时,改变水平滑动条的值 */
    horizontalSlider->setSliderPosition(val);
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,会显示水平和垂直滑动条

14. QKeySequenceEdit

QKeySequenceEdit 继承 QWidget ,该小部件允许用户选择 QKeySequence, 通常用作快捷方式。当小部件接收到焦点并在用户释放最后一个键后一秒结束时,将启动记录,常用作记录快捷键

以下实例实现的效果:自定义快捷键

  • 创建新项目,注意不勾选 “Generate form”,默认继承 QMainWindow 类
  • 头文件 “mainwindow.h” 中具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QKeySequenceEdit>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    /* 声明QKeySequenceEdit对象 */
    QKeySequenceEdit  *keySequenceEdit;

private slots:
    /* 声明槽 */
    void KSEKeySequenceChanged(const QKeySequence &);

};
#endif // MAINWINDOW_H
  • 源文件 “mainwindow.cpp” 中具体代码如下
#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    /* 主窗体设置位置与大小 */
    this->setGeometry(0, 0, 800, 480);

    /* 实例化 */
    keySequenceEdit = new QKeySequenceEdit(this);

    /* 设置位置与大小 */
    keySequenceEdit->setGeometry(350, 200, 150, 30);

    /* 信号槽连接 */
    connect(keySequenceEdit,
            SIGNAL(keySequenceChanged(const QKeySequence &)),
            this,
            SLOT(KSEKeySequenceChanged(const QKeySequence &))
            );

}

MainWindow::~MainWindow()
{
}

void MainWindow::KSEKeySequenceChanged(
        const QKeySequence &keySequence)
{
    /* 判断输入的组合键是否为Ctrl + Q,如果是则退出程序 */
    if(keySequence == QKeySequence(tr("Ctrl+Q"))) {
        /* 结束程序 */
        this->close();
    }else {
        /* 打印出按下的组合键 */
        qDebug()<<keySequence.toString()<<endl;
    }
}
  • 源文件 “main.cpp” 代码,由新建项目时生成,无需改动
  • 程序编译及运行后,当焦点在 KeySequenceEdit 里时,按下键盘里的任一键或者组合键,一秒后,KeySequenceEdit 将记录了这个 这组组合键,并打印在输出信息里。直到程序按下Ctrl + Q 组合键后,程序窗口才关闭,结束
举报

相关推荐

0 条评论