一起看代码来玩玩QT之05 Controls

pixmap  : 像素图

combo : 组合


QObject::connect(&button,SIGNAL(clicked()),&w,SLOT(close()));
此连接信号槽 可用 c++  lamboda表达式
 connect(radio, &QRadioButton::clicked, [](bool v){
        qDebug() << v;
    });
 这需要  在 项目pro 中 加    config += C++11

内容有点多,请诸位 一个一个控件看,最好亲手打一遍!!!!!!! 一个一个控件 试!!!!!!!!!


.h 文件

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include 
#include 
class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);

    QTextEdit* _edit;

signals:

public slots:
    void slotComboBoxIndexChanged(const QString&);
};

#endif // MYWIDGET_H

.cpp 文件

#include "MyWidget.h"
#include 

#include      // disply static text, or image
#include 
#include  // single line

#include 
#include 
#include 
#include  // multi lines, rich text
#include  // readonly textedit
#include   //
#include     // 模拟方式显示数值
#include   // 数字
#include 
#include 
#include 
#include  // 不介绍,不算控件
#include 
#include 
#include 
#include 
#include  
#include 

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("baidu"));
    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(""
                  ""
                  ""
                  "
head1head2
value1value2
value3value4

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



你可能感兴趣的:(C++,Qt,代码)