qt控件学习(2)

QComboBox 下拉列表框,QFontComboBox 字体下拉列表框

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QComboBox>
#include <QFontComboBox>
#include <QPushButton>
#include <QLabel>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QComboBox *comboBox;
    QFontComboBox *fontComboBox;
    QPushButton *button;
    QLabel *label;
//按钮点击方法
private slots:
    void txtButton();

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("test app");
    this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    this->setAttribute(Qt::WA_TranslucentBackground, true);
//    this->setStyleSheet("background:red");

//    initial
    comboBox = new QComboBox(this);
    fontComboBox = new QFontComboBox(this);
    button = new QPushButton(this);
    label = new QLabel(this);


//    position
    comboBox->setGeometry(QRect(0,0,120,25));
    label->setGeometry(QRect(50,150,300,25));
    button->move(130,50);
    fontComboBox->setGeometry(QRect(0,50,120,25));
//    shuxi
    QStringList str;
    str<<"math"<<"chinese"<<"geometry";
    comboBox->addItems(str);
    button->setText("按钮");

//    affair
    connect(button,SIGNAL(released()),this,SLOT(txtButton()));

}

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

void MainWindow::txtButton()
{
 label->setText("选择字体:"+fontComboBox->currentText());
}

效果图
效果图


QSpinBox

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QSpinBox>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QSpinBox *spinBox;

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("test app");
    this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    this->setAttribute(Qt::WA_TranslucentBackground, true);
//    this->setStyleSheet("background:red");

    //实例 QSpinBox
    spinBox = new QSpinBox(this);
    //位置
    spinBox->setGeometry(QRect(50,50,100,25));
    //值范围
    spinBox->setRange(0,200);
    //初始值
    spinBox->setValue(10);
    //后缀
    spinBox->setSuffix("元");
    //前缀
    spinBox->setPrefix("$");

}

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

效果图
效果图

QTimeEdit 时间控件&&QDateEdit 日期控件

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QTimeEdit>
#include <QDateEdit>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QTimeEdit *timeEdit;
    QDateEdit *dateEdit;


};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("test app");
    this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    this->setAttribute(Qt::WA_TranslucentBackground, true);
//    this->setStyleSheet("background:red");

    //实例
    timeEdit = new QTimeEdit(this);
    //位置
    timeEdit->setGeometry(QRect(50,50,200,25));
    //获取系统时间
    QDateTime sysTime = QDateTime::currentDateTime();
    //获取时分秒以“:”号拆分赋予 list 数组
    QStringList list = sysTime.toString("hh:mm:ss").split(':');
    //将时分秒绑定控件
    timeEdit->setTime(QTime(list[0].toInt(),list[1].toInt(),list[2].toInt()));


    //实例
    dateEdit = new QDateEdit(this);
    //位置
    dateEdit->setGeometry(QRect(50,75,120,25));
    //获取系统时间
    QDateTime sysTime1 = QDateTime::currentDateTime();
    //获取时分秒以“-”号拆分赋予 list 数组
    QStringList list1 = sysTime1.toString("yyyy-MM-dd").split('-');
    //将年月日绑定控件
    dateEdit->setDate(QDate(list1[0].toInt(),list1[1].toInt(),list1[2].toInt()));

}

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

效果图
效果图

QScrollBar 控件——滚动条

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QScrollBar>
#include <QSpinBox>



QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QScrollBar *scrollBar;
    QSpinBox *spinBox;


};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("test app");
    this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    this->setAttribute(Qt::WA_TranslucentBackground, true);
//    this->setStyleSheet("background:red");

    //实例
    scrollBar = new QScrollBar(this);
    spinBox = new QSpinBox(this);
    //横显/竖显
    scrollBar->setOrientation(Qt::Horizontal);
    //位置
    scrollBar->setGeometry(QRect(50,50,180,20));
    spinBox->setGeometry(QRect(50,90,100,25));
    //滚动条宽度
    scrollBar->setPageStep(10);
    //scrollBar 事件
    connect(scrollBar,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));
    //spinBox 事件
    connect(spinBox,SIGNAL(valueChanged(int)),scrollBar,SLOT(setValue(int)));
    //初始值
    scrollBar->setValue(50);

}

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

效果图
效果图

QRadioButton 单选按钮 && QCheckBox 复选框

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QRadioButton>
#include <QCheckBox>
#include <QLabel>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
private slots:
    void checkChange();


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

private:
    Ui::MainWindow *ui;
    QRadioButton *radioM;
    QRadioButton *radioW;
    QCheckBox *checkBox01;
    QCheckBox *checkBox02;
    QCheckBox *checkBox03;
    QLabel *label;


};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("test app");
    this->setWindowIcon(QIcon(":/new/prefix1/images/favicon.ico"));
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    this->setAttribute(Qt::WA_TranslucentBackground, true);
//    this->setStyleSheet("background:red");

    //实例
    radioM = new QRadioButton(this);
    radioW = new QRadioButton(this);
    label = new QLabel(this);
    //位置
    radioM->setGeometry(QRect(50,50,50,50));
    radioW->setGeometry(QRect(100,50,50,50));
    radioM->setText("男");
    radioW->setText("女");
    //默认选择
    radioM->setChecked(true);

    //实例化 checkBox 控件
    checkBox01 = new QCheckBox(this);
    checkBox02 = new QCheckBox(this);
    checkBox03 = new QCheckBox(this);
    //实例 label 控件显示结果
    label = new QLabel(this);
    //控件位置
    checkBox01->setGeometry(QRect(50,100,50,50));
    checkBox02->setGeometry(QRect(100,100,50,50));
    checkBox03->setGeometry(QRect(150,100,50,50));
    label->setGeometry(QRect(50,150,200,30));
    //控件值
    checkBox01->setText("数学");
    checkBox02->setText("语文");
    checkBox03->setText("地理");
    //checkbox 控件点击事件
    connect(checkBox01, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
    connect(checkBox02, SIGNAL(clicked(bool)), this, SLOT(checkChange()));
    connect(checkBox03, SIGNAL(clicked(bool)), this, SLOT(checkChange()));



}

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

//定义接收变量
QString str;
void MainWindow::checkChange()
{
    if (sender() == checkBox01)
    {
        //判断是否被选中
        if (checkBox01->checkState() == Qt::Checked)
        {
            str += "数学";
        }
        else
        {
            str = str.replace(QString("数学"), QString(""));
        }
    }
    else if (sender() == checkBox02)
    {
        if (checkBox02->checkState() == Qt::Checked)
        {
            str += "语文";
        }
        else
        {
            str = str.replace(QString("语文"), QString(""));
        }
    }
    else if (sender() == checkBox03)
    {
        if (checkBox03->checkState() == Qt::Checked)
        {
            str += "地理";
        }
        else
        {
            str = str.replace(QString("地理"), QString(""));
        }
    }
    //绑定值
    label->setText(str);
}


效果图
效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值