lv20 QT 常用控件 2

1 QT GUI 类继承简介

布局管理器

输出控件

输入控件

按钮

容器

2 按钮示例

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void setpass(bool flag);
private:
    QLineEdit *le;
    QCheckBox *ck;
    QPushButton *pb;



};



#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    le = new QLineEdit;
    le->setEchoMode(QLineEdit::Password);   //显示模式为密码
    ck = new QCheckBox("show Password");
    pb = new QPushButton("clear");

    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(le);
    vbox->addWidget(ck);
    vbox->addWidget(pb);
    this->setLayout(vbox);

    connect(pb, SIGNAL(clicked(bool)), le , SLOT(clear()));
    connect(ck, SIGNAL(clicked(bool)), this , SLOT(setpass(bool)));  //点击chechbox会显示密码,setpass(bool) 是接收信号的对象的槽函数,所以用this
}

void Widget::setpass(bool flag)
{
    if(!flag)
        le->setEchoMode(QLineEdit::Password);
    else
        le->setEchoMode(QLineEdit::Normal);
}

Widget::~Widget()
{

}

效果

3 容器

3.1 Group box

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QGroupBox>
#include <QRadioButton>

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    QGroupBox *gb;

    QRadioButton *r1, *r2, *r3;
};

#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    gb = new QGroupBox("choice", this);

    r1 = new QRadioButton("aaaa");
    r2 = new QRadioButton("bbbb");
    r3 = new QRadioButton("cccc");

    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(r1);
    vbox->addWidget(r2);
    vbox->addWidget(r3);

    gb->setLayout(vbox);

    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addWidget(gb);
    this->setLayout(mainbox);
}

Widget::~Widget()
{

}

效果

3.2 ScrollArea

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>
#include <QScrollArea>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
    QLabel *lb;
    QScrollArea *sa;
};

#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //创建一个 QLabel 控件,并将一张名为 "1.jpg" 的图片显示在该 QLabel 上。
    lb = new QLabel(this);
    QPixmap pix("1.jpg");  //加载图片
    lb->setPixmap(pix);

    sa = new QScrollArea(this);
    sa->setWidget(lb);

    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addWidget(sa);
    this->setLayout(mainbox);
}

Widget::~Widget()
{

}

效果

3.3 TabWidget

管理多个标签页(Tab)的容器控件。它提供了一个便捷的方式来显示和切换多个页面

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTabWidget>
#include <QTextEdit>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
    QTabWidget *tw;
    QTextEdit *te1, *te2, *te3;
};

#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    tw = new QTabWidget;

    te1 = new QTextEdit("11111111");
    te2 = new QTextEdit("112221111");
    te3 = new QTextEdit("11113333");

    tw->addTab(te1, "1.c");
    tw->addTab(te2, "2.c");
    tw->addTab(te3, "3.c");

    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addWidget(tw);
    this->setLayout(mainbox);
}

Widget::~Widget()
{

}

效果

3.4 StackedWidget

堆叠窗口控件,可以用来管理多个子部件,但一次只能显示其中一个子部件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QStackedWidget>
#include <QTextEdit>
#include <QComboBox>

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    QStackedWidget *sw;

    QTextEdit *te1, *te2, *te3;

    QComboBox *cb;

};

#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    sw = new QStackedWidget;

    te1 = new QTextEdit("aaaa");
    te2 = new QTextEdit("aaadsfasdfasdfa");
    te3 = new QTextEdit("aaasdfaaa");

    sw->addWidget(te1);
    sw->addWidget(te2);
    sw->addWidget(te3);

    cb = new QComboBox;
    cb->addItem("1111");
    cb->addItem("2222");
    cb->addItem("3333");

    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addWidget(sw);
    mainbox->addWidget(cb);
    this->setLayout(mainbox);

    connect(cb, SIGNAL(activated(int)), sw, SLOT(setCurrentIndex(int)));
}

Widget::~Widget()
{

}

效果

3.5 Toolbox

折叠面板控件,通常用于显示多个分组的相关内容

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QToolBox>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
    QToolBox *tb;

    QPushButton *pb[3];
};

#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    tb = new QToolBox;

    pb[0] = new QPushButton("aaa");
    pb[1] = new QPushButton("bbb");
    pb[2] = new QPushButton("ccc");

    tb->addItem(pb[0], "11111");
    tb->addItem(pb[1], "22222");
    tb->addItem(pb[2], "33333");

    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addWidget(tb);
    this->setLayout(mainbox);
}

Widget::~Widget()
{

}

效果(类似消息框)

相关推荐
Felix_One17 小时前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
dustcell.9 天前
haproxy七层代理
java·开发语言·前端
norlan_jame9 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone9 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054969 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月9 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237179 天前
C语言-数组练习进阶
c语言·开发语言·算法