码迷,mamicode.com
首页 > 其他好文 > 详细

2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl

时间:2014-12-12 20:58:55      阅读:750      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   使用   sp   on   


1.新建一个空项目(其它项目—>QT项目):

2  添加新文件(选择C++Class

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

MyWidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
 
#include <QWidget>
#include <QTextEdit>
class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);
 
    QTextEdit* _edit;
 
signals:
 
public slots:
    void slotComboBoxIndexChanged(const QString&);
};
 
#endif // MYWIDGET_H

05Controls.pro文件的内容

HEADERS += \
    MyWidget.h
 
SOURCES += \
    MyWidget.cpp
QT += widgets gui
CONFIG += C++11

MyWidget.cpp

#include "MyWidget.h"

#include <QApplication>

 

#include <QLabel>         // disply static text, or image

#include <QPushButton>

#include <QLineEdit>      // single line

 

#include <QComboBox>      //下拉选

#include <QCheckBox>      //复选框

#include <QRadioButton>   //单选按钮

#include <QTextEdit>      // multi lines, rich text

#include <QTextBrowser>   // readonly textedit

#include <QGroupBox>

#include <QSlider>        // 模拟方式显示数值

#include <QSpinBox>       // 数字

#include <QDateEdit>

#include <QTimeEdit>

#include <QDateTimeEdit>

#include <QTabWidget>     // 不介绍,不算控件

#include <QCompleter>

#include <QHBoxLayout>    //布局控件

#include <QDebug>         //这里用于测试用

#include <QPixmap>

#include <QVBoxLayout>

#include <QLCDNumber>

 

MyWidget::MyWidget(QWidget *parent) :

    QWidget(parent)

{

    QVBoxLayout* lay = new QVBoxLayout(this);

    QLabel* label;

    QComboBox* combo;

    QPushButton* button;

    QRadioButton* radio;

    QTextEdit* edit;

    QGroupBox* group;

 

    //说明可以通过编写样式的方式修改GUI中字体的样式

    lay->addWidget(label = new QLabel("<a href=www.baidu.com>百度</a>"));

    //通过下面一句在lable上添加一个图片,当加了下面一句之后,上面的baidu字就会被覆盖

    //label->setPixmap(QPixmap("../aaa.png"));

 

    //当选中了lable之后会,输出字符串

    connect(label, &QLabel::linkActivated, [](QString str){

        qDebug() << str;

    });

 

    lay->addWidget(button = new QPushButton("Button"));

    //button添加样式

    button->setStyleSheet("QPushButton {font:bold 16px; color:red; padding:5px}");

 

    lay->addWidget(radio = new QRadioButton("Radio"));

    radio->setStyleSheet("QRadioButton {font:bold 16px; color:red; padding:5px}");

    //为单选按钮添加事件,true或者false

    connect(radio, &QRadioButton::clicked, [](bool v){

        qDebug() << v;

    });

 

    lay->addWidget(new QCheckBox("check box"));

    //添加下拉选

    lay->addWidget(combo = new QComboBox());

    combo->addItem("Select item1");

    combo->addItem("Select item2");

    //设置下拉选可以被编辑

    combo->setEditable(true);

    //当下拉选selectChanged之后,用信号槽进行处理

    connect(combo, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotComboBoxIndexChanged(QString)));

    combo->setCompleter(new QCompleter(combo->model()));

 

    //说明多行文本框之中可以使用html标签页面

    lay->addWidget(edit = new QTextEdit);

    edit->setText("<table border=1><tr><th>head1</th><th>head2</th></tr>"

                  "<tr><td>value1</td><td>value2</td></tr>"

                  "<tr><td>value3</td><td>value4</td></tr>"

                  "</table><br>"

                  "<img src=../aaa.png></img>");

    _edit = edit;

    connect(edit, &QTextEdit::textChanged, [&](){

     //   QTextEdit* edit = (QTextEdit*)this->sender();

        qDebug() << _edit->toPlainText();

    });

    //设置竖直方向上有水平滚动条

    edit->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    //设置水平方向上没有滚动条

    edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

 

    lay->addWidget(group = new QGroupBox("Some items"));

    QHBoxLayout* hBoxLay;

    group->setLayout(hBoxLay = new QHBoxLayout);

    hBoxLay->addWidget(new QPushButton("aaaa"));

    hBoxLay->addWidget(new QPushButton("aaaa"));

 

    QSlider* slider;

    QSpinBox* spinBox;

 

    lay->addWidget(slider = new QSlider(Qt::Horizontal));

    slider->setMaximum(100);

    slider->setMinimum(0);

 

    lay->addWidget(spinBox = new QSpinBox);

    spinBox->setMaximum(100);

    spinBox->setMinimum(0);

 

    connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));

    connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));

 

    lay->addWidget(new QDateTimeEdit);

 

    //设置LCD样式的显示状态

    QLCDNumber* lcd;

    lay->addWidget(lcd = new QLCDNumber(10));

    lcd->display(12345);

    lcd->setMode(QLCDNumber::Hex);

    lcd->setSegmentStyle(QLCDNumber::Outline);

}

 

void MyWidget::slotComboBoxIndexChanged(const QString &str)

{

    qDebug() << "slotComboBoxIndexChanged" << str;

}

 

int main(int argc, char** argv)

{

    QApplication app(argc, argv);

 

    MyWidget w;

    w.show();

 

    return app.exec();

}

运行结果:

bubuko.com,布布扣

 

2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl

标签:style   blog   http   io   ar   color   使用   sp   on   

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/41897973

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!