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

二、Qt常用控件

时间:2018-08-04 20:29:36      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:sele   bug   控件   span   vbo   max   stat   :link   object   

#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
#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;

    lay->addWidget(label = new QLabel("<a href=www.baidu.com>baidu</a>"));
    label->setPixmap(QPixmap("../aaa.png"));

    connect(label, &QLabel::linkActivated, [](QString str){
        qDebug() << str;
    });

    lay->addWidget(button = new QPushButton("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}");
    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);
    connect(combo, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotComboBoxIndexChanged(QString)));

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

    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);

    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();
}

 

二、Qt常用控件

标签:sele   bug   控件   span   vbo   max   stat   :link   object   

原文地址:https://www.cnblogs.com/zch-blog/p/9419410.html

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