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()
{

}

效果(类似消息框)

相关推荐
zz-zjx1 天前
云原生LVS+Keepalived高可用方案(二)
开发语言·php·lvs
wuwu_q1 天前
用通俗易懂 + Android 开发实战的方式,详细讲解 Kotlin Flow 中的 retryWhen 操作符
android·开发语言·kotlin
网络精创大傻1 天前
PHP 与 Node.js:实际性能对比
开发语言·node.js·php
snakecy1 天前
过关斩将编程题
开发语言·python
diannao7201 天前
实时将大模型的解决方案转换为随机应变的机器人指令
开发语言·windows·python·机器人
Nebula_g1 天前
C语言应用实例:斐波那契数列与其其他应用
c语言·开发语言·后端·学习·算法
梅梅绵绵冰1 天前
SpringAOP的相关概念
java·开发语言
Xiaoyu Wang1 天前
GC垃圾回收
java·开发语言·jvm
陈佳梁1 天前
构造器(详解)
java·开发语言
Jonathan Star1 天前
在 JavaScript 中, `Map` 和 `Object` 都可用于存储键值对,但设计目标、特性和适用场景有显著差异。
开发语言·javascript·ecmascript