Qt之常見控件[傳智播客聽課筆記]

#include "widget.h"

#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>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout* lay = new QVBoxLayout(this);


    QPushButton* button;


    QLabel* label;
    lay->addWidget(label = new QLabel("<a href=www.baidu.com>baidu</a>"));
    connect(label, &QLabel::linkActivated, [](QString str){
           qDebug() << str;
       });
   // label->setPixmap(QPixmap("../qtblog.png"));
    QRadioButton* radio;
    lay->addWidget(radio = new QRadioButton("Radio"));
    radio->setStyleSheet("QRadioButton {font:bold 16px; color:red; padding:5px}");
    connect(radio, &QRadioButton::clicked, [](bool v){
            qDebug() << v;
        });

    QCheckBox *check;
    lay->addWidget(check = new QCheckBox("check box"));
    connect(check, &QCheckBox::clicked, [](bool v){
            qDebug() << v;
        });

    QComboBox* combo;
    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()));

    QTextEdit* edit;    //能夠在QTextEdit中顯示圖片
    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=../test0005.jpg></img>"
                 );
    edit->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);  //添加滾動條
    edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//水平方向滾動條關閉

//    _edit = edit;
//    connect(edit, &QTextEdit::textChanged, [&](){
//     //   QTextEdit* edit = (QTextEdit*)this->sender();
//        qDebug() << _edit->toPlainText();
//    });

    QGroupBox* group;
    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)));

    QLCDNumber* lcd;
    lay->addWidget(lcd = new QLCDNumber(10));
    lcd->display(12345);
    lcd->setMode(QLCDNumber::Hex);
    lcd->setSegmentStyle(QLCDNumber::Outline);
}

void Widget::slotComboBoxIndexChanged(const QString & str)
{
    qDebug() << "slotComboBoxIndexChanged" << str;
}

相關文章
相關標籤/搜索