Qt常用控件介绍(一)

5 篇文章 11 订阅

Qt Creator 的使用技巧

Qt Creator的常用快捷键

在这里插入图片描述在这里插入图片描述

按钮

Qt按钮部件是一种常用的部件之一,Qt内置六种按钮部件如下:
在这里插入图片描述
(1) QPushButton:下压按钮
(2) QToolButton:工具按钮
(3) QRadioButton:选择按钮
(4) QCheckBox:检查框
(5) QCommandLinkButton:命令链接按钮
(6) QDialogButtonBox:对话框按钮
这六种按钮部件作用简介如下:
QPushButton 继承 QAbstractButton 类,被 QCommandLinkButton 继承。通常用于执行命令或触发事件。
QToolButton 继承 QAbstractButton 类。是一种用于命令或者选项的可以快速访问的按钮,通常在 ToolBar 里面。工具按钮通常显示的是图标,而不是文本标签。 ToolButton 支持自动浮起。在自动浮起模式中,按钮只有在鼠标指向它的时候才绘制三维的框架。
QRadioButton 继承 QAbstractButton 类。 RadioButton 单选按钮(单选框)通常成组出现,用于提供两个或多个互斥选项。
QCheckBox 继承 QAbstractButton。复选按钮(复选框)与 RadioButton 的区别是选择模式,单选按钮提供多选一,复选按钮提供多选多。
QCommandLinkButton 控件中文名是“命令链接按钮”。QCommandLinkButton 继承 QPushButton。 QCommandLinkButton 控件和 RadioButton 相似,都是用于在互斥选项中选择一项。表
面上同平面按钮一样,但是 CommandLinkButton 除带有正常的按钮上的文字描述文本外,默认情况下,它也将携带一个箭头图标,表明按下按钮将打开另一个窗口或页面。
QDialogButtonBox 按 钮 盒 子 ( 按 钮 框 ), 是 由QDialogButtonBox 类 包 装 成 的 。
QDialogButtonBox 继承 QWidget。常用于对话框里自定义按钮,比如“确定”和“取消” 按钮。

QPushButton

//不完全代码
/* 引入 QPushButton 类 ,添加头文件*/
#include <QPushButton>
private:
/* 声明一个 QPushButton 对象 pushButton1 */
QPushButton *pushButton1;
/* 声明一个 QPushButton 对象 pushButton2 */
QPushButton *pushButton2;

private slots:
/* 声明对象 pushButton1 的槽函数 */
 void pushButton1_Clicked();
 /* 声明对象 pushButton2 的槽函数 */
 void pushButton2_Clicked();



/* mainwindow.cpp代码添加如下*/
/* 设置宽高为 800×480,位置在 0, 0。(0, 0)代表原点, Qt 默认最左上角的点为原点 */
this->setGeometry(0, 0, 800, 480);
/* 实例化两个按钮对象,并设置其显示文本为窗口皮肤 1 和窗口皮肤 2 */
pushButton1 = new QPushButton("窗口皮肤 1", this);
pushButton2 = new QPushButton("窗口皮肤 2", this);
/* 设定两个 QPushButton 对象的位置 */
pushButton1->setGeometry(300,200,80,40);
pushButton2->setGeometry(400,200,80,40);

/* 信号槽连接 */
connect(pushButton1, SIGNAL(clicked()), this,SLOT(pushButton1_Clicked()));
connect(pushButton2, SIGNAL(clicked()), this,SLOT(pushButton2_Clicked()));


/* 槽函数的实现 */
 void MainWindow::pushButton1_Clicked() {
 /* 设置主窗口的样式 1 */
this->setStyleSheet("QMainWindow { background-color:rgba(255, 245,238, 100%); }");
}
/* 槽函数的实现 */
 void MainWindow::pushButton2_Clicked()
  {
 /* 设置主窗口的样式 2 */
this->setStyleSheet("QMainWindow { background-color:rgba(100, 255,100, 100%); }");
//设置background-color 的 rgba 参数即可改变窗体的背景颜色

}
/*************
rgba() 函数使用红®、绿(g)、蓝(b)、透明度(a)的叠加来生成各式各样的颜色。

rgba 即红色、绿色、蓝色、透明度(red, green, blue, alpha)
在对应的位置就代表对应的颜色以及透明度

红色(r)0 到 255 间的整数,代表颜色中的红色成分
绿色(g)0 到 255 间的整数,代表颜色中的绿色成分
蓝色(b)0 到 255 间的整数,代表颜色中的蓝色成分
透明度(a)取值 0~1 之间, 代表透明度
***************/

运行效果如下:
在这里插入图片描述

QToolButton

工具按钮(QToolButton)区别于普通按钮(QPushButton)的一点是,工具按钮(QToolButton)可以带图标。这里区别下图标和按钮的背景图片是不一样的。通常我们在 QToolBar 这种工具条(工具栏)上设置不同的按钮。

/* 引入 QToolButton 类 */
#include <QToolButton>
/* 引入 QToolBar 类 */
#include <QToolBar>

private:
/* 声明一个 QToolButton 对象 */
QToolButton *toolButton;
/* 声明一个 QToolBar 对象 */
QToolBar *toolBar;


/****************mainwindow.cpp 编程添加代码***********/
#include <QStyle>
/* 实例化 QToolBar 对象 */
toolBar = new QToolBar(this);
/* 设置 toolBar 的位置和大小 */
toolBar->setGeometry(0, 0, 800, 100);

/* 实例化 QStyle 类对象,用于设置风格,调用系统类自带的图标 */
QStyle *style = QApplication::style();

/* 使用 Qt 自带的标准图标,可以在帮助文档里搜索 QStyle::StandardPixmap */
QIcon icon =style->standardIcon(QStyle::SP_TitleBarContextHelpButton);

/* 实例化 QToolButton 对象 */
toolButton = new QToolButton();
 /* 设置图标 */
toolButton->setIcon(icon);
/* 设置要显示的文本 */
 toolButton->setText("帮助");
/* 调用 setToolButtonStyle()方法,设置 toolButoon 的样式,设置为文本置于
图标下方 */
 toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

/* 最后将 toolButton 添加到 ToolBar 里 */
 toolBar->addWidget(toolButton);

运行效果如下所示:
在这里插入图片描述

QRadioButton

QRadioButton 部件提供了一个带有文本标签的单选框(单选按钮)。
QRadioButton 是一个可以切换选中(checked)或未选中(unchecked)状态的选项按钮。单选框通常呈现给用户一个“多选一”的选择。也就是说,在一组单选框中,一次只能选中一个单选框。默认在同一个父对象下,初始化后点击它们是互斥状态。

/* 引入 QRadioButton */
#include <QRadioButton>
private:
/* 声明两个 QRadioButton 对象 */
QRadioButton *radioButton1;
QRadioButton *radioButton2;
/****************mainwindow.cpp 编程添加代码***********/
this->setStyleSheet("QMainWindow {background-color: rgba(200, 50,100, 100%);}");
/* 实例化对象 */
radioButton1 = new QRadioButton(this);
radioButton2 = new QRadioButton(this);

/* 设置两个 QRadioButton 的位置和显示大小 */
radioButton1->setGeometry(300, 200, 100, 50);
radioButton2->setGeometry(400, 200, 100, 50);

/* 设置两个 QRadioButton 的显示文本 */
radioButton1->setText("开关一");
radioButton2->setText("开关二");

/* 设置初始状态, radioButton1 的 Checked 为 false,另一个为 true*/
radioButton1->setChecked(false);
radioButton2->setChecked(true);

/****************main.cpp 编程添加代码***********/
#include <QFile>
 /* 指定文件 */
    QFile file(":/style.qss");

    /* 判断文件是否存在 */
    if (file.exists() ) {
        /* 以只读的方式打开 */
        file.open(QFile::ReadOnly);
        /* 以字符串的方式保存读出的结果 */
        QString styleSheet = QLatin1String(file.readAll());
        /* 设置全局样式 */
        qApp->setStyleSheet(styleSheet);
        /* 关闭文件 */
        file.close();
    }
 /****************qss文件 编程添加代码***********/   
    QRadioButton{
    spacing: 2px;
    color: white;
}
QRadioButton::indicator {
    width: 45px;
    height: 30px;
}
QRadioButton::indicator:unchecked {
    image: url(:/images/switch_off.png);
}
QRadioButton::indicator:checked {
    image: url(:/images/switch_on.png);
}

运行效果如下所示:
在这里插入图片描述

QCheckBox

QCheckBox 继承 QAbstractButton。复选按钮(复选框)与 RadioButton 的区别是选择模式,单选按钮提供多选一,复选按钮提供多选多。

/* 引入 QCheckBox */
#include <QCheckBox>
private:
 /* 声明一个 QCheckBox 对象 */
 QCheckBox *checkBox;
private slots:
 /* 声明 QCheckBox 的槽函数,并带参数传递,用这个参数接收信号的参数 */
void checkBoxStateChanged(int);
/****************mainwindow.cpp 编程添加代码***********/
this->setStyleSheet("QMainWindow {background-color: rgba(100, 100, 100, 100%);}");

    /* 实例化对象 */
    checkBox = new QCheckBox(this);

    /* 设置QCheckBox位置和显示大小 */
    checkBox->setGeometry(350, 200, 250, 50);

    /* 初始化三态复选框的状态为Checked */
    checkBox->setCheckState(Qt::Checked);

    /* 设置显示的文本 */
    checkBox->setText("初始化为Checked状态");

    /* 开启三态模式,必须开启,否则只有两种状态,即Checked和Unchecked */
    checkBox->setTristate();

    /* 连接checkBox的信号stateChanged(int),与我们定义的槽checkBoxStateChanged(int)连接 */
    connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxStateChanged(int)));
/* 槽函数的实现 */
void MainWindow::checkBoxStateChanged(int state)
{
    /* 判断checkBox的state状态,设置checkBox的文本 */
    switch (state) {
    case Qt::Checked:
        /* 选中状态 */
        checkBox->setText("Checked状态");
        break;
    case Qt::Unchecked:
        /* 未选中状态 */
        checkBox->setText("Unchecked状态");
        break;
    case Qt::PartiallyChecked:
        /* 半选状态 */
        checkBox->setText("PartiallyChecked状态");
        break;
    default:
        break;
    }
}
/****************main.cpp 编程添加代码***********/
#include <QFile>
 /* 指定文件 */
    QFile file(":/style.qss");

    /* 判断文件是否存在 */
    if (file.exists() ) {
        /* 以只读的方式打开 */
        file.open(QFile::ReadOnly);
        /* 以字符串的方式保存读出的结果 */
        QString styleSheet = QLatin1String(file.readAll());
        /* 设置全局样式 */
        qApp->setStyleSheet(styleSheet);
        /* 关闭文件 */
        file.close();
    }
 /****************qss文件 编程添加代码***********/   
QCheckBox{
        spacing: 5px;
        color: white;
}
QCheckBox::indicator {
        width: 50px;
        height: 50px;
}
QCheckBox::indicator:enabled:unchecked {
        image: url(:/images/unchecked.png);
}
QCheckBox::indicator:enabled:checked {
        image: url(:/images/checked.png);
}
QCheckBox::indicator:enabled:indeterminate {
        image: url(:/images/indeterminate.png);
}

运行效果如下:
在这里插入图片描述

QCommandLinkButton

QCommandLinkButton 控件中文名是“命令链接按钮 ”。 QCommandLinkButton 继承QPushButton。CommandLinkButton 控件和 RadioButton 相似,都是用于在互斥选项中选择一项。表面上同平面按钮一样,但是 CommandLinkButton 除带有正常的按钮上的文字描述文本外,默认情况下,它也将携带一个箭头图标,表明按下按钮将打开另一个窗口或页面。

/* 引入QCommandLinkButton */
#include <QCommandLinkButton>
private:
    /* 声明一个QCommandLinkButton对象 */
    QCommandLinkButton *commandLinkButton;

private slots:
    /* 声明槽函数,用于点击commandLinkButton后触发 */
    void commandLinkButtonClicked();
/****************mainwindow.cpp 编程添加代码***********/
/* 引入桌面服务,用来打开系统文件夹对话框 */
#include <QDesktopServices>
/* 引入QUrl */
#include <QUrl>
/* 实例化对象 */
   commandLinkButton = new QCommandLinkButton(
                   "打开桌面目录", "点击此将调用系统的窗口打开桌面目录",this);

    /* 设置QCommandLinkButton位置和显示大小 */
    commandLinkButton->setGeometry(300, 200, 250, 60);

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

void MainWindow::commandLinkButtonClicked()
{
    /* 调用系统服务打开/home目录 */
  QDesktopServices::openUrl(QUrl("C:/Users/12645/Desktop") );
}
 

运行效果如下:
在这里插入图片描述

QDialogButtonBox

对话框和消息框通常以符合该平台界面指导原则的布局呈现按钮。不同平台的对话框总是有不同的布局。 QDialogButtonBox 允许开发人员向其添加按钮,并将自动使用适合用户桌面环境的布局。 也就是说我们可以使用系统的自带的对话框按钮,也可以自己定义对话框按钮。
在这里插入图片描述

/* 引入QDialogButtonBox */
#include <QDialogButtonBox>
/* 引入QPuhsButton */
#include <QPushButton>
private:
    /* 声明一个QDialogButtonBox对象 */
    QDialogButtonBox *dialogButtonBox;

    /* 声明一个QPushButton 对象 */
    QPushButton *pushButton;

private slots:
    /* 声明信号槽,带QAbstractButton *参数,用于判断点击了哪个按钮 */
    void dialogButtonBoxClicked(QAbstractButton *);
/****************mainwindow.cpp 编程添加代码***********/
/* 引入QDebug */
#include <QDebug>
/* 实例化并设置按钮的盒子的大小和位置 */
    dialogButtonBox = new  QDialogButtonBox(this);
    dialogButtonBox->setGeometry(300, 200, 200, 30);

    /*使用Qt的Cancel按钮*/
    dialogButtonBox->addButton(QDialogButtonBox::Cancel);

    /*将英文"Cancel"按钮设置为中文"取消" */
    dialogButtonBox->button(QDialogButtonBox::Cancel)->setText("取消");

    /* 设定位置与大小 */
    pushButton = new QPushButton(tr("自定义"));

    /* 将pushButton添加到dialogButtonBox,并设定ButtonRole为ActionRole */
    dialogButtonBox->addButton(pushButton, QDialogButtonBox::ActionRole);

    /* 信号槽连接,带参数QAbstractButton *,用于判断用户点击哪个按键 */
    connect(dialogButtonBox, SIGNAL(clicked(QAbstractButton * )),
            this, SLOT(dialogButtonBoxClicked(QAbstractButton *)));
            
void MainWindow::dialogButtonBoxClicked(QAbstractButton *button)
{
    /* 判断点击的对象是否为QDialogButtonBox::Cancel */
    if(button == dialogButtonBox->button(QDialogButtonBox::Cancel)) {
        /* 打印“单击了取消键” */
        qDebug() <<"单击了取消键"<<endl;
        /* 判断点击的对象是否为pushButton */
    }else if(button == pushButton) {
        /* 打印“单击了自定义键” */
        qDebug() <<"单击了自定义键"<<endl;
    }
}

在这里插入图片描述
运行效果如下:
在这里插入图片描述

按钮部件源码下载地址:
https://download.csdn.net/download/Liuzhu_shusheng_DH/19954254

输入窗口部件

Qt Designer 窗口部件提供的面板中,提供了 16 种输入部件如下
在这里插入图片描述
( 1) Comb Box:组合框
( 2) Font Comb Box:字体组合框
( 3) Line Edit:单行编辑框
( 4) Text Edit:文本编辑框
( 5) Plain Text Edit:纯文本编辑框
( 6) Spin Box:数字旋转框
( 7) Double Spin Box:双精度数字旋转框
( 8) Time Edit:时间编辑框
( 9) Date Edit:日期编辑框
( 10) Date/Time Edit:日期时间编辑框
( 11) Dial:数字拨盘框
( 12) Horizontal Scroll Bar:水平滚动条
( 13) Vertical Scroll Bar:垂直滚动条
( 14) Horizontal Slider:水平滑动条
( 15) Vertical Slider:垂直滑动条
( 16) Key sequence Edit:按键序列编辑框
这十六种按钮部件作用简介如下:
QComboBox 继承 QWidget 类,被 QFontComboBox 类继承。通常用于用户显示选项列表的方法, 这种方法占用最少的屏幕空间。
QFontComboBox 继承 QComboBox。
QFontComboBox 小部件是一个允许用户选择字体系列的组合框。 组合框中填充了按字母顺序排列的字体家族名称列表。 FontComboBox 常用于工具栏,与 ComboBox 一起用于控制字体大小,并与两个 ToolButtons 一起用于粗体和斜体。
QLineEdit 继承 QWidget。 QLineEdit 小部件是一个单行文本编辑器。行编辑允许用户使用一组有用的编辑函数输入和编辑一行纯文本,包括撤消和重做、剪切和粘贴以及拖放。通过更改行编辑的 echoMode(),它还可以用作“只写”字段,用于输入如密码等。
QTextEdit 继承 QAbstractScrollArea,被 QTextBrowser 继承。 QTextEdit 是一个高级所见即所得查看器/编辑器,支持使用 html 样式的标记进行 rich text 格式化。它经过优化以处理大型
文档并快速响应用户输入。 QTextEdit 用于段落和字符。段落是格式化的字符串,它被字包装以适应小部件的宽度。在阅读纯文本时,默认情况下,一个换行表示一个段落。一份文件由零个
或多个段落组成。段落中的文字与段落的对齐方式一致。段落之间用硬换行符隔开。段落中的每个字符都有自己的属性,例如字体和颜色。 QTextEdit 可以显示图像,列表和表格。如果文本太大而无法在文本编辑的视图中查看,视图中则会出现滚动条。
QPlainTextEdit 是一个支持纯文本的高级查看器/编辑器。它被优化为处理大型文档和快速响应用户输入。
QSpinBox 继承 QAbstractSpinBox。用于处理整数和离散值(例如:月份名称)而 QDoubleSpinBox 则用于处理浮点值。他们之间的区别就是处理数据的类型不同,其他功能都基本相同。QSpinBox 允许用户通过单击上/下按钮或按下键盘上的上/下按钮来选择一个值,以增加/减少当前显示的值。用户还可以手动输入值。
QDoubleSpinBox 继承 QAbstractSpinBox。 QDoubleSpinBox 则用于处理浮点值。 QDoubleSpinBox 允许用户通过单击“向上”和“向下”按钮或按下键盘上的“向上”或“向下”按钮来选择当前显示的值。用户还可以手动输入值。
QTimeEdit 继承 QDateTimeEdit。 QTimeEdit 用于编辑时间,而 QDateEdit 用于编辑日期。
QDateEdit 继承 QDateTimeEdit。 QDateEdit 用于编辑日期,而 QTimeEdit 用于编辑时间。
QDateTimeEdit 类提供了一个用于编辑日期和时间的小部件。 QDateTimeEdit 允许用户使用键盘或箭头键编辑日期,以增加或减少日期和时间值。箭头键可用于在 QDateTimeEdit 框中从一个区域移动到另一个区域。
QDial 类提供了一个圆形范围控制(如速度计或电位器)。 QDial 用于当用户需要在可编程定
义的范围内控制一个值,并且该范围要么是环绕的(例如,从 0 到 359 度测量的角度),要么对话框布局需要一个正方形小部件。由于 QDial 从 QAbstractSlider 继承,因此拨号的行为与滑块
类似。 当 wrapping()为 false(默认设置)时,滑块和刻度盘之间没有真正的区别。 它们共享相同的信号,插槽和成员功能。 您使用哪一个取决于您的用户期望和应用程序类型。
QScrollBar 继承 QAbstractSlider。 QScrollBar 小部件提供垂直或水平滚动条,允许用户访问比用于显示文档的小部件大的文档部分。它提供了用户在文档中的当前位置和可见文档数量的可视化指示。滚动条通常配有其他控件,可以实现更精确的导航。
QSlider 继承 QAbstractSlider。 QSlider 类提供垂直或水平滑动条小部件, 滑动条是用于控制有界值的典型小部件。它允许用户沿着水平或垂直凹槽移动滑块手柄,并将手柄的位置转换为合法范围内的整数值。
QKeySequenceEdit 继承 QWidget。这个小部件允许用户选择 QKeySequence, QKeySequence通常用作快捷方式。当小部件接收到焦点并在用户释放最后一个键后一秒结束时,将启动记录,通常用作记录快捷键。

QComboBox

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

/* 引入QComboBox */
#include <QComboBox>
private:
    /* 声明一个QComboBox对象 */
    QComboBox *comboBox;

private slots:
    /* 声明QComboBox对象的槽函数 */
     void comboBoxIndexChanged(int);
/****************mainwindow.cpp 编程添加代码***********/
/* 引入QDebug */
#include <QDebug>
/* 设置主窗体的显示位置与大小 */
    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)));
        
void MainWindow::comboBoxIndexChanged(int index)
{
    /* 打印出选择的省份 */
    qDebug()<<"您选择的省份是"<< comboBox->itemText(index)<<endl;
}

运行效果如下所示:
在这里插入图片描述

QFontComboBox

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

/* 引入QFontComboBox */
#include <QFontComboBox>
/* 引入QLable */
#include <QLabel>
private:
    /* 声明一个QComboBox对象 */
    QComboBox *comboBox;

private:
    /* 声明一个QFontComboBox对象 */
    QFontComboBox *fontComboBox;
    /* 声明一个Label对象,用于显示当前字体变化 */
    QLabel *label;

private slots:
    /* 声明QFontComboBox对象使用的槽函数 */
    void fontComboBoxFontChanged(QFont);
/****************mainwindow.cpp 编程添加代码***********/
/* 引入QDebug */
#include <QDebug>
/* 设置主窗体的显示位置与大小 */
    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)));
        
void MainWindow::fontComboBoxFontChanged(QFont font)
{
    /* 将label里的文本内容设置为所选择的字体 */
    label->setFont(font);

    /* 定义一个字符串接收当前项的字体 */
    QString str = "用此标签显示字体效果\n设置的字体为:" +
            fontComboBox->itemText(fontComboBox->currentIndex());

    /* 将字符串的内容作为label的显示内容 */
    label->setText(str);
}

运行效果如下所示:
在这里插入图片描述

QLineEdit

QLineEdit 小部件是一个单行文本编辑器。行编辑允许用户使用一组有用的编辑函数输入和编辑一行纯文本。 包括撤消和重做、剪切和粘贴以及拖放。通过更改行编辑的 echoMode(),它还可以用作“只写”字段,用于输入如密码等。


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

private:
    /* 声明一个QLineEdit对象 */
    QLineEdit *lineEdit;

    /* 声明一个QPushButton对象 */
    QPushButton *pushButton;

    /* 声明一个QLabel对象 */
    QLabel *label;

private slots:
    /* 声明一个槽函数,响应pushButton的clicked事件 */
    void pushButtonClicked();
/****************mainwindow.cpp 编程添加代码***********/
/* 引入QDebug */
#include <QDebug>
/* 设置主窗体的显示位置与大小 */
    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()));
        
void MainWindow::pushButtonClicked()
{
    /* 字符串变量str */
    QString str;

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

    /* 设置label里的文本显示内容 */
    label->setText(str);
    /* 在点击了确认键之后清空lineEdit单行输入框 */
    lineEdit->clear();
}

运行效果如下所示:
在这里插入图片描述

QTextEdit

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

#include <QTextEdit>
#include <QPushButton>

private:
    /* 声明一个QTextEdit对象 */
    QTextEdit *textEdit;

    /* 声明两个QPushButton对象 */
    QPushButton *pushButtonSelectAll;
    QPushButton *pushButtonClearAll;

private slots:
    /* 声明两个槽函数,响应按钮点击响应的事件 */
    void pushButtonSelectAllClicked();
    void pushButtonClearAllClicked();
/****************mainwindow.cpp 编程添加代码***********/
/* 引入QDebug */
#include <QDebug>
/* 设置主窗体的显示位置与大小 */
    this->setGeometry(0, 0, 800, 480);
	/* 实例和对象,设置位置和显示大小 */
    textEdit = new QTextEdit(this);
    textEdit->setGeometry(0, 0, 800, 400);

    /* 实例和对象,设置位置和显示大小,设置文本 */
    pushButtonSelectAll = new QPushButton(this);
    pushButtonSelectAll->setGeometry(200, 420, 50, 20);
    pushButtonSelectAll->setText("全选");

    /* 实例和对象,设置位置和显示大小,设置文本 */
    pushButtonClearAll = new QPushButton(this);
    pushButtonClearAll->setGeometry(500, 420, 50, 20);
    pushButtonClearAll->setText("清除");

    /* 信号槽连接 */
    connect(pushButtonSelectAll, SIGNAL(clicked()), this,
            SLOT(pushButtonSelectAllClicked()));
    connect(pushButtonClearAll, SIGNAL(clicked()), this,
            SLOT(pushButtonClearAllClicked()));
        
void MainWindow::pushButtonSelectAllClicked()
{
    /* 设置焦点为textEdit */
    textEdit->setFocus();//当我们在文本输入框里输入完成后,当点击全选按钮后,需要设置焦点到
textEdit 上,否则将不能设置全选。
    /* 判断文本编辑框内容是否为空,不为空则全选 */
    if(!textEdit->toPlainText().isEmpty()){
        /* 全选 */
        textEdit->selectAll();
    }
}

void MainWindow::pushButtonClearAllClicked()
{
    /* 清空textEdit里的文本内容 */
    textEdit->clear();
}

运行效果如下所示:
在这里插入图片描述

QPlainTextEdit

QPlainTextEdit 类提供了一个用于编辑和显示纯文本的小部件,常用于显示多行文本或简单文本。QPlainTextEdit 可以理解为 QTextEdit 的低配版。 QPlainTextEdit 支持纯文本显示, QTextEdit 支持富文本(支持多种格式,比如插入图片,链接等)显示。就是多一个样式。 QPlainTextEdit 显示的效率比 QTextEdit 高,如果需要显示大量文字,尤其是需要滚动条来回滚动的时候, QPlainTextEdit 要好很多。

#include <QPlainTextEdit>
#include <QRadioButton>

private:
    /* 声明对象 */
    QPlainTextEdit *plainTextEdit;
    QRadioButton *radioButton;

private slots:
    /* 槽函数 */
    void radioButtonClicked();
/****************mainwindow.cpp 编程添加代码***********/
#include <QDir>
#include <QTextStream>
#include <QCoreApplication>
/* 设置当前程序的工作目录为可执行程序的工作目录 */
    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()));
        
void MainWindow::radioButtonClicked()
{
    /* 检查radioButton是否选中 */
    if(radioButton->isChecked()) {
        /* 设置为只读模式 */
        plainTextEdit->setReadOnly(true);
    } else {
        /* 设置为非只读模式 */
        plainTextEdit->setReadOnly(false);
    }
}

运行效果如下所示:

在这里插入图片描述

QSpinBox

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

#include <QSpinBox>
private:
/* 声明一个 QSpinBox 对象 */
QSpinBox *spinBox;
private slots:
/* 槽函数 */
void spinBoxValueChanged(int);
/****************mainwindow.cpp 编程添加代码***********/
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)));
 void MainWindow::spinBoxValueChanged(int opacity)
{
    /* 转换为double数据类型 */
    double  dobleopacity = (double)opacity / 100;

    /* 设置窗体不透明度,范围是0.0~1.0。1则为不透明,0为全透明 */
    this->setWindowOpacity(dobleopacity);
}

运行效果如下所示:
在这里插入图片描述

QDoubleSpinBox

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

#include <QDoubleSpinBox>
private:
    /* 声明一个QDoubleSpinBox对象 */
    QDoubleSpinBox *doubleSpinBox;

private slots:
    /* 槽函数 */
    void doubleSpinBoxValueChanged(double);
/****************mainwindow.cpp 编程添加代码***********/
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)));
 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);

}

运行效果如下所示:
在这里插入图片描述

QTimeEdit,QDateEdit,QDateTimeEdit

QTimeEdit 类提供一个基于 QDateTimeEdit 类编辑时间的小部件。
QDateEdit 类提供一个基于 QDateTimeEdit 类编辑日期的小部件。
从名字可知 QDateTimeEdit 类提供了一个用于编辑日期和时间的小部件。 QDateTimeEdit允许用户使用键盘或箭头键编辑日期,以增加或减少日期和时间值。箭头键可用于在QDateTimeEdit 框中从一个区域移动到另一个区域。 实际上是 QDateTimeEdit 和 QDateEdit 的组
合。

#include <QDateTimeEdit>
#include <QTimeEdit>
#include <QDateEdit>
private:
    /* 声明对象 */
    QDateTimeEdit *dateTimeEdit;
    QTimeEdit *timeEdit;
    QDateEdit *dateEdit;

/****************mainwindow.cpp 编程添加代码***********/
 /* 设置位置与大小 */
    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);

运行效果如下所示:
在这里插入图片描述

QDial

QDial 类提供了一个圆形范围控制(如速度计或电位器)。 QDial 用于当用户需要在可编程定义的范围内控制一个值,并且该范围要么是环绕的(例如,从 0 到 359 度测量的角度),要么对话框布局需要一个正方形小部件。由于 QDial 从 QAbstractSlider 继承,因此拨号的行为与滑块类似。 当 wrapping()为 false(默认设置)时,滑块和刻度盘之间没有真正的区别。 它们共享相同的信号,插槽和成员功能。 您使用哪一个取决于您的用户期望和应用程序类型。

#include <QDial>
#include <QLabel>
private:
    /* 声明对象 */
    QDial *dial;
    QLabel *label;

private slots:
    /* 槽函数 */
    void dialValueChanged(int);
/****************mainwindow.cpp 编程添加代码***********/
/* 设置主窗体的位置与大小 */
    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)));
void MainWindow::dialValueChanged(int val)
{
    /* QString::number()转换成字符串 */
    label->setText(QString::number(val) + "km/h");
}

运行效果如下所示:
在这里插入图片描述

QScrollBar

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

#include <QScrollBar>
#include <QLabel>
private:
    /* 声明水平滚动条对象 */
    QScrollBar *horizontalScrollBar;

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

    /* 声明标签文本 */
    QLabel *label;
/****************mainwindow.cpp 编程添加代码***********/
  /* 设置主窗体大小,位置 */
    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);

运行效果如下所示:
在这里插入图片描述

QSlider

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

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

/****************mainwindow.cpp 编程添加代码***********/
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)));
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);
}

运行效果如下所示:
在这里插入图片描述

QKeySequenceEdit

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

#include <QKeySequenceEdit>
private:
    /* 声明QKeySequenceEdit对象 */
    QKeySequenceEdit  *keySequenceEdit;

private slots:
    /* 声明槽 */
    void KSEKeySequenceChanged(const QKeySequence &);
/****************mainwindow.cpp 编程添加代码***********/
 /* 主窗体设置位置与大小 */
    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 &))
            );
void MainWindow::KSEKeySequenceChanged(
        const QKeySequence &keySequence)
{
    /* 判断输入的组合键是否为Ctrl + Q,如果是则退出程序 */
    if(keySequence == QKeySequence(tr("Ctrl+Q"))) {
        /* 结束程序 */
        this->close();
    }else {
        /* 打印出按下的组合键 */
        qDebug()<<keySequence.toString()<<endl;
    }
}

运行效果如下所示:
在这里插入图片描述
输入窗口部件的例程链接:
链接: link.

  • 56
    点赞
  • 366
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

六竹书生__wa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值