Qt函数及库的运用
Nevermore 2025-07-03  OS
# 常用控件
# 布局
- 表单布局QFormLayout
 
qFLaoyout = new QFormLayout(this);
le1 = new QLineEdit(this);
le2 = new QLineEdit(this);
le3 = new QLineEdit(this);
qFLaoyout->addRow("name", le1);
qFLaoyout->addRow("age", le2);
qFLaoyout->addRow("birth", le3);
qFLaoyout->setRowWrapPolicy(QFormLayout::WrapAllRows);
qFLaoyout->setLabelAlignment(Qt::AlignLeft);
setWindowTitle("test");
 1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- 网格布局QGridLayout
 
qGLaoyout = new QGridLayout(this);
qGLaoyout->setContentsMargins(0, 0, 0, 0);
qGLaoyout->setSpacing(10);
bt1 = new QPushButton(this);
bt1->setText("Button1");
bt1->setFixedHeight(40);
bt1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
bt2 = new QPushButton(this);
bt2->setText("Button2");
bt2->setFixedHeight(40);
bt2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
bt3 = new QPushButton(this);
bt3->setText("Button3");
bt3->setFixedWidth(100);
bt3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
qGLaoyout->addWidget(bt1, 0, 1);
qGLaoyout->addWidget(bt2, 1, 1);
qGLaoyout->addWidget(bt3, 0, 0, 2, 1); //行 列 占行 占列
QHBoxLayout *phb = new QHBoxLayout();
phb->addWidget(new QPushButton("hello"));
phb->addWidget(new QPushButton("world"));
phb->setSpacing(20);
QVBoxLayout *pvb = new QVBoxLayout();
QLabel *pl = new QLabel("test");
QLabel *pl2 = new QLabel("test2");
pvb->addWidget(pl);
pvb->addWidget(pl2);
qGLaoyout->addLayout(phb, 2, 0);
qGLaoyout->addLayout(pvb, 0, 2);
setLayout(qGLaoyout);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- QListWidget
 
QListWidget *pl = new QListWidget(this);
pl->insertItem(0, "test0");
pl->insertItem(1, "test1");
pl->insertItem(2, "test2");
QStackedWidget *pSw = new QStackedWidget(this);
pSw->setGeometry(100, 20, 80, 80);
pSw->addWidget(new QLabel("table0"));
pSw->addWidget(new QLabel("table1"));
pSw->addWidget(new QLabel("table2"));
connect(pl, &QListWidget::currentRowChanged, this, [ = ](int index)
        {
            pSw->setCurrentIndex(index);
        });
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 按键
- QToolButton
 
tbar = new QToolBar(this);
tbar->setGeometry(10, 10, 100, 80);
tbar->setStyleSheet("QToolBar { background-color: #FF0000; }"); // 红色背景
QStyle *sty = QApplication::style();
QIcon ico = sty->standardIcon(QStyle::SP_TitleBarContextHelpButton);
toolbt = new QToolButton(this);
toolbt->setText("hint");
toolbt->setIcon(ico);
toolbt->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
tbar->addWidget(toolbt);
 1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- QRadioButton
 
QRadioButton *rad1 = new QRadioButton(this);
rad1->setText("btn1");
rad1->setGeometry(20, 20, 100, 40);
QRadioButton *rad2 = new QRadioButton(this);
rad2->setText("btn2");
rad2->setGeometry(20, 80, 100, 40);
//default
rad1->setChecked(true);
rad2->setChecked(false);
 1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- QCheckBox
 
QCheckBox *cb = new QCheckBox(this);
cb->setGeometry(20, 20, 200, 80);
cb->setCheckState(Qt::Checked);
cb->setText("initial state");
cb->setTristate();// tree state mode(checked unchecked PartiallyChecked)
 1
2
3
4
5
6
2
3
4
5
6
- QCommandLinkButton
 
QCommandLinkButton * clb = new QCommandLinkButton("test", "clicked", this);
clb->setGeometry(20, 20, 200, 80);
 1
2
2
- QDialogButtonBox
 
QDialogButtonBox* dbx = new QDialogButtonBox(this);
dbx->setGeometry(20, 20, 200, 80);
dbx->addButton(QDialogButtonBox::Cancel);
dbx->button(QDialogButtonBox::Cancel)->setText("real Cancel");
QPushButton *pbtn = new QPushButton("action", this);
dbx->addButton(pbtn, QDialogButtonBox::ActionRole);
// connect(dbx, SIGNAL(clicked(QAbstractButton*)), this, SLOT(f(QAbstractButton*)) );
connect(dbx, &QDialogButtonBox::clicked, this,  [ = ](QAbstractButton * bt)
        {
            if(bt == dbx->button(QDialogButtonBox::Cancel))
            {
                qDebug() << "clicked cancel";
            }
            else if(bt == pbtn)
            {
                qDebug() << "clicked test";
            }
        });
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 视图
- listview
 
QListView* plv = new QListView(this);
plv->setGeometry(20, 20, 200, 180);
QStringList qlist;
qlist.append("name: zhangsan");
qlist.append("sex: male");
qlist.append("age:18");
QStringListModel *lm = new QStringListModel(qlist);
plv->setModel(lm);
QObject::connect(plv, &QListView::clicked, this, [](const QModelIndex &index)
                 {
                     qDebug() << index.data().toString();
                 });
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- treeview
 
QTreeView *pTv = new QTreeView(this);
QStandardItemModel * psItem = new QStandardItemModel(pTv);
psItem->setHorizontalHeaderLabels(QStringList() << QStringLiteral("Number") << QStringLiteral("Zone"));
// first class
QList<QStandardItem*> sitem1;
QStandardItem *item11 = new QStandardItem(QString::number(1));
QStandardItem *item12 = new QStandardItem("Beijing");
sitem1.append(item11);
sitem1.append(item12);
psItem->appendRow(sitem1);
QList<QStandardItem*> sitem11;
QStandardItem *item111 = new QStandardItem(QString::number(1));
QStandardItem *item112 = new QStandardItem("ChaoYao");
sitem11.append(item111);
sitem11.append(item112);
item11->appendRow(sitem11);
QList<QStandardItem*> sitem111;
QStandardItem *item1111 = new QStandardItem(QString::number(1));
QStandardItem *item1112 = new QStandardItem("one unit");
sitem111.append(item1111);
sitem111.append(item1112);
item111->appendRow(sitem111);
pTv->setModel(psItem);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- tableview
 
QTableView * pTv = new QTableView(this);
pTv->setGeometry(20, 20, 400, 300);
QStandardItemModel * psItem = new QStandardItemModel(pTv);
psItem->setHorizontalHeaderItem(0, new QStandardItem("Number"));
psItem->setHorizontalHeaderItem(1, new QStandardItem("name"));
psItem->setHorizontalHeaderItem(2, new QStandardItem("gender"));
pTv->setModel(psItem);
pTv->setColumnWidth(1, 150);
pTv->setRowHeight(0, 100);
psItem->setItem(0, 0, new QStandardItem("1999"));
psItem->setItem(0, 1, new QStandardItem("Zhangsan"));
psItem->setItem(0, 2, new QStandardItem("male"));
psItem->setItem(1, 0, new QStandardItem("2998"));
psItem->setItem(1, 1, new QStandardItem("lisi"));
psItem->setItem(1, 2, new QStandardItem("male"));
pTv->setEditTriggers(QAbstractItemView::NoEditTriggers); // cannnot be edited
psItem->sort(0, Qt::DescendingOrder);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- listwidget
 
QListWidgetItem *qItem = new QListWidgetItem("Deaths Of Flowers");
qItem->setTextAlignment(Qt::AlignHCenter);
QListWidget *pLw = new QListWidget(this);
QStringList slit;
slit << "I would if I could choose";
slit << "Age and die outwards as a tulip does;";
slit << "Not as this iris drawing in, in-coiling";
pLw->addItem(qItem);
pLw->addItems(slit);
 1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- treewidget
 
QTreeWidget *pTw = new QTreeWidget(this);
QTreeWidgetItem *pTitem = new QTreeWidgetItem(pTw);
pTitem->setText(0, "test");
pTitem->setCheckState(0, Qt::Checked);
pTw->addTopLevelItem(pTitem);
pTw->setHeaderHidden(true);
pTw->expandAll();
// second class
QTreeWidgetItem *pTitem1 = new QTreeWidgetItem(pTitem);
pTitem1->setText(0, "maco");
pTitem1->setCheckState(0, Qt::Checked);
 1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- tablewidget
 
QTableWidget *pTw = new QTableWidget(this);
pTw->setRowCount(2);
pTw->setColumnCount(2);
QStringList slist;
slist << "Number" << "Age";
pTw->setHorizontalHeaderLabels(slist);
QList<QString> strNo;
strNo << "20200001" << "20200002";
QList<QString> strAge;
strAge << "16" << "17";
for(int i = 0; i < 2; i++)
{
    int j = 0;
    QTableWidgetItem *pItem = new QTableWidgetItem(strNo.at(i));
    pTw->setItem(i, j++, pItem);
    pTw->setItem(i, j++, new QTableWidgetItem(strAge.at(i)));
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- QGraphicsView
 
- 视图 -> 场景
 - 场景 -> 视图
 - 场景 -> 图元 QGraphicsItem
 - 图元 ->场景
 - 子图元 -> 父图元
 - 父图元 ->子图元
 - 本图元 -> 其他图元
 - 其他图元 -> 本图元
 
# 容器
- QGroupBox
 
QGroupBox * pGb1 = new QGroupBox("Single Choose");
QRadioButton *pRbtn1 = new QRadioButton("r1");
QRadioButton *pRbtn2 = new QRadioButton("r2");
QRadioButton *pRbtn3 = new QRadioButton("r3");
QVBoxLayout *pVL = new QVBoxLayout();
pVL->addWidget(pRbtn1);
pVL->addWidget(pRbtn2);
pVL->addWidget(pRbtn3);
pGb1->setLayout(pVL);
QGroupBox * pGb2 = new QGroupBox("Muti Choose");
//pGb2->setCheckable(true);
QCheckBox *pCb1 = new QCheckBox("checkbox1");
QCheckBox *pCb2 = new QCheckBox("checkbox2");
QCheckBox *pCb3 = new QCheckBox("checkbox3");
QVBoxLayout *pVL2 = new QVBoxLayout();
pVL2->addWidget(pCb1);
pVL2->addWidget(pCb2);
pVL2->addWidget(pCb3);
pGb2->setLayout(pVL2);
QGridLayout *pGL = new QGridLayout(this);
pGL->addWidget(pGb1, 0, 0, 1, 1);
pGL->addWidget(pGb2, 0, 1, 1, 1);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- QScrollArea
 
QLabel *qLb = new QLabel();
qLb->setScaledContents(true);
QImage image(800, 600, QImage::Format_RGB32);
image.fill(Qt::green);
QPainter painter(&image);
QRect rect(100, 100, 200, 150);
painter.drawRect(rect);
painter.end();
qLb->setPixmap(QPixmap::fromImage(image));
QScrollArea *pSA = new QScrollArea(this);
pSA->setWidgetResizable(true); //auto size
pSA->setAlignment(Qt::AlignCenter);
pSA->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
pSA->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
pSA->setWidget(qLb);
QGridLayout* pgl = new QGridLayout;
pgl->addWidget(pSA);
this->setLayout(pgl);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- QTabWidget
 
QTabWidget *pTw = new QTabWidget(this);
pTw->setGeometry(20, 20, 400, 300);
pTw->show();
QWidget *qWd1 = new QWidget();
pTw->addTab(qWd1, "tab1");
QGridLayout *pGL = new QGridLayout;
QLabel *lab1 = new QLabel("intput");
QLineEdit *ple = new QLineEdit();
pGL->addWidget(lab1, 0, 0);
pGL->addWidget(ple, 0, 1);
qWd1->setLayout(pGL);
QWidget *qWd2 = new QWidget();
pTw->addTab(qWd2, "tab2");
QWidget *qWd3 = new QWidget();
pTw->addTab(qWd3, "tab3");
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- QFrame
 
QFrame *pfm = new QFrame(this);
pfm->setGeometry(20, 20, 200, 200);
pfm->setStyleSheet("background-color:green");
pfm->setLineWidth(2);
pfm->setMidLineWidth(2);
pfm->setFrameShape(QFrame::Box);
 1
2
3
4
5
6
2
3
4
5
6
- QDockWidget
 
QDockWidget *pDw = new QDockWidget("Dock Widget", this);
pDw->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable);
pDw->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
QTextEdit *qedit = new QTextEdit();
qedit->setText("11111111111111111");
pDw->setWidget(qedit);
pDw->setMaximumSize(300, 300);
 1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- QSplitter
 
QSplitter *psl = new QSplitter(Qt::Horizontal, this);
QTextEdit *pt = new QTextEdit("left window", psl);
QSplitter *psr = new QSplitter(Qt::Vertical, psl);
QTextEdit *tsru = new QTextEdit("up window", psr);
QTextEdit *tsrd = new QTextEdit("down window", psr);
 1
2
3
4
5
6
2
3
4
5
6
# 输入
- QComboBox
 
QComboBox * pCb = new QComboBox(this);
pCb->setGeometry(20, 20, 100, 40);
pCb->addItem("test1");
pCb->addItem("test2");
pCb->addItem("test3");
connect(pCb, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [ = ](int index)
        {
            qDebug() << pCb->itemText(index);
        });
 1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- QFontComboBox
 
QFontComboBox *pFc = new QFontComboBox(this);
QLabel *plb = new QLabel("hello", this);
pFc->setGeometry(20, 60, 100, 40);
plb->setGeometry(20, 100, 100, 40);
connect(pFc, &QFontComboBox::currentFontChanged, this, [ = ](QFont font)
        {
            plb->setFont(font);
        });
 1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- QLineEdit
 
QLineEdit *pLe = new QLineEdit("hello", this);
pLe->setGeometry(20, 20, 100, 40);
qDebug() << pLe->text();
pLe->clear();
 1
2
3
4
2
3
4
- QPlainTextEdit
 
QPlainTextEdit * pt = new QPlainTextEdit(this);
pt->setGeometry(20, 20, 400, 300);
QDir::setCurrent(QCoreApplication::applicationDirPath());
QFile fe("test.txt");
fe.open(QFile::ReadOnly | QFile::Text); //只读
QTextStream strin(&fe);
pt->insertPlainText(strin.readAll());
pt->setReadOnly(true);
 1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- QSpinBox
 
QSpinBox* psb = new QSpinBox(this);
psb->setGeometry(20, 20, 100, 40);
psb->setRange(0, 100);
psb->setSingleStep(10);
psb->setValue(100);
psb->setSuffix("%");
this->setStyleSheet("QWidget{background-color:""rgba(200,200,100,100%)}");
connect(psb, QOverload<int>::of(&QSpinBox::valueChanged), this, [ = ](int x)
{
double dx = x * 1.0 / 100;
this->setWindowOpacity(dx);
});
 1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- QTimeEdit 时间
 
QTimeEdit *pTe = new QTimeEdit(QTime::currentTime(), this);
pTe->setGeometry(10, 10, 200, 40);
QDateTimeEdit *pDte = new QDateTimeEdit(QDateTime::currentDateTime(), this);
pDte->setGeometry(10, 80, 200, 40);
QDateEdit *pDe = new QDateEdit(QDate::currentDate(), this);
pDe->setGeometry(10, 140, 200, 40);
 1
2
3
4
5
6
2
3
4
5
6
- QScrollBar 滚轮
 
QScrollBar *phs = new QScrollBar(Qt::Horizontal, this);
phs->setGeometry(0, 0, 1000, 40);
QScrollBar *pvs = new QScrollBar(Qt::Vertical, this);
pvs->setGeometry(0, 0, 40, 1000);
 1
2
3
4
2
3
4
- QKeySequenceEdit 组合键
 
QKeySequenceEdit *pKs = new QKeySequenceEdit(this);
pKs->setGeometry(20, 20, 200, 30);
connect(pKs, &QKeySequenceEdit::keySequenceChanged, this, [ = ](const QKeySequence &key)
        {
            qDebug() << key;
            if(key == QKeySequence("Ctrl+Q"))
            {
                this->close();
            }
        });
 1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- QColorDialog 颜色
 
QPushButton *btn = new QPushButton("color", this);
QFrame *frame = new QFrame(this);
frame->setGeometry(100, 100, 100, 100);
frame->setFrameShape(QFrame::Box);
frame->setAutoFillBackground(true);
connect(btn, &QPushButton::clicked, this, [ = ]()
        {
            QColor col = QColorDialog::getColor(Qt::red);
            frame->setPalette(QPalette(col));
        });
 1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- QFontDialog 字体
 
QHBoxLayout *hbox = new QHBoxLayout();
QLineEdit *pl = new QLineEdit();
pl->setText("testHUagqisjsdfs");
hbox->addWidget(pl);
QPushButton *btn = new QPushButton("choose", this);
hbox->addWidget(btn);
connect(btn, &QPushButton::clicked, this, [ = ]()
        {
            bool ret;
            QFont font = QFontDialog::getFont(&ret);
            if(ret)
            {
                pl->setFont(font);
            }
        });
this->setLayout(hbox);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- QInputDialog
 
QHBoxLayout *hbox = new QHBoxLayout();
QLineEdit *pl = new QLineEdit();
pl->setText("testHUagqisjsdfs");
hbox->addWidget(pl);
QPushButton *btn = new QPushButton("choose", this);
hbox->addWidget(btn);
connect(btn, &QPushButton::clicked, this, [ = ]()
        {
            QStringList strItem;
            strItem << "test1" << "test2";
            bool ret;
            // QString str =  QInputDialog::getText(this, "title", "input", QLineEdit::Normal, pl->text(), &ret);
            QString str =  QInputDialog::getItem(this, "title", "input", strItem, 0, false, &ret);
            if(ret && !str.isEmpty())
            {
                pl->setText(str);
            }
        });
this->setLayout(hbox);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- 自定义消息框
 
QGridLayout *gl = new QGridLayout(this);
QLabel *lb = new QLabel("message");
QPushButton *btn = new QPushButton("click", this);
gl->addWidget(lb, 0, 0);
gl->addWidget(btn, 0, 1);
connect(btn, &QPushButton::clicked, this, [ = ]()
        {
            QMessageBox msg;
            msg.setWindowTitle("test");
            QPushButton *yes = msg.addButton("Yes", QMessageBox::ActionRole);
            QPushButton *no = msg.addButton("No", QMessageBox::ActionRole);
            msg.setIconPixmap(QPixmap());
            msg.exec();
            if(msg.clickedButton() == yes)
            {
                lb->setText("yes");
            }
            else
            {
                lb->setText("no");
            }
        });
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 事件处理
# 鼠标事件
status = new QLabel();
status->setText("status");
status->setFixedWidth(200);
QStatusBar *sbar = new QStatusBar(this);
sbar->addPermanentWidget(status);
this->setMouseTracking(true);
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    status->setText("(" + QString::number(event->x()) + "," + QString::number(event->y()) + ")");
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 键盘事件
void Widget::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Right)
    {
        qDebug() << "Key_Right";
    }
}
 1
2
3
4
5
6
7
2
3
4
5
6
7
# 事件过滤
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == lab1)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *me = (QMouseEvent*)event;
            if(me->button() & Qt::LeftButton)
            {
                lab1->setText("hello");
            }
        }
    }
    return QWidget::eventFilter(watched, event);
}
image = QImage(300, 400, QImage::Format_RGB32);
image.fill(Qt::green);
QPainter painter(&image);
QRect rect(100, 100, 200, 150);
painter.drawRect(rect);
painter.end();
lab1 = new QLabel(this);
lab1->setScaledContents(true);
lab1->setGeometry(20, 20, 300, 300);
lab1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
lab1->setPixmap(QPixmap::fromImage(image));
lab1->installEventFilter(this);
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 文件
- 获取文件属性
 
QHBoxLayout *pL = new QHBoxLayout(this);
QLineEdit *edit = new QLineEdit();
QPushButton *btn = new QPushButton("click", this);
pL->addWidget(edit);
pL->addWidget(btn);
connect(btn, &QPushButton::clicked, this, [ = ]()
        {
            QString filePath = QFileDialog::getOpenFileName(this, "open", "/", "files(*)");
            edit->setText(filePath);
            QFileInfo qfi(filePath);
            qint64 filesize = qfi.size();
            QDateTime createTime = qfi.birthTime();
            QDateTime lastTime = qfi.lastModified();
            QDateTime lastAccess = qfi.lastRead();
            qDebug() << filesize << "K" << "-" << "create Time" << createTime << "-" << "last modified" << lastTime;
        });
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- 读写文件
 
QFile
QDataStream 读写二进制
QTextStream 读写文本
QFile qfs("./tst.txt");
if(!qfs.open(QIODevice::WriteOnly | QIODevice::Text))
{
    qDebug() << "open failed";
}
else
{
    qDebug() << "open OK";
}
QTextStream qst(&qfs);
qst << "hello";
qfs.close();
if(!qfs.open(QIODevice::ReadOnly | QIODevice::Text))
{
    qDebug() << "Open Failed";
}
else
{
    QTextStream readTsr(&qfs);
    while(!readTsr.atEnd())
    {
        QString tmp;
        readTsr >> tmp;
        qDebug() << tmp;
    }
    qfs.close();
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 绘图
setAutoFillBackground(true);
QPalette plet = this->palette();
plet.setColor(QPalette::Window, Qt::white);
setPalette(plet);
setMinimumSize(800, 600);
setMaximumSize(800, 600);
img = new QImage(20, 20, QImage::Format_RGBA64);
for(int i = 0; i < img->width(); i++)
{
    for(int j = 0; j < img->height(); j++)
    {
        img->setPixel(i, j, qRgb(255, 0, 0));
    }
}
img->setText("hello", "what");
pic = new QPixmap(this->size().width(), this->size().height());  //注意初始化大小
pic->fill(Qt::green);
QPainter *painter = new QPainter(this);
QPen pen(Qt::DashDotLine);
for(int i = 0; i < this->size().width(); i += 10)
{
    painter->begin(pic);
    painter->setPen(pen);
    painter->drawLine(QPoint(i, 0), QPoint(i, this->size().height()));
    painter->end();
}
for(int i = 0; i < this->size().height(); i += 10)
{
    painter->begin(pic);
    painter->setPen(pen);
    painter->drawLine(QPoint(0, i), QPoint(this->size().width(), i));
    painter->end();
}
painter->begin(pic);
painter->drawImage(QPoint(10, 10), *img);
painter->end();
resize(800, 600);
void Widget::paintEvent(QPaintEvent *event)
{
    QPainter pt(this);
    pt.begin(this);
    pt.drawPixmap(QPoint(0, 0), *pic);
    pt.end();
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 网络库
- QHostInfo / QNetworkInterface
 - 获取本地主机名称和ip
 
#include <QHostInfo>
#include <QNetworkInterface>
void Widget::GetHostNameAndIp()
{
    QString strLocal = QHostInfo::localHostName();  //Get Host PC Name
    QString strLoacalAddr = "";  // Get Host PC IP addr by name
    QHostInfo hostInfo = QHostInfo::fromName(strLocal);
    QList<QHostAddress> ipaddrList = hostInfo.addresses();
    if(!ipaddrList.isEmpty())
    {
        for(int i = 0; i < ipaddrList.size(); i++)
        {
            QHostAddress addrHost = ipaddrList.at(i);
            if(QAbstractSocket::IPv4Protocol == addrHost.protocol())
            {
                strLoacalAddr = addrHost.toString();
                break;
            }
        }
    }
    qDebug() << strLocal << strLoacalAddr;
    QString temp = "";
    QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
    for(int i = 0; i < netList.size(); i++)
    {
        QNetworkInterface interfaces = netList.at(i);
        qDebug() << "设备名称:" << interfaces.name();
        qDebug() << "硬件地址:" << interfaces.hardwareAddress();
        QList<QNetworkAddressEntry> entrylist =  interfaces.addressEntries();
        for(int j = 0; j < entrylist.size(); j++)
        {
            QNetworkAddressEntry enter = entrylist.at(j);
            qDebug() << "IP地址:" << enter.ip().toString();
            qDebug() << "子网掩码:" << enter.netmask().toString();
            qDebug() << "广播地址:" << enter.broadcast().toString();
        }
    }
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
- 获取域名对应的主机ip
 
QString Widget::ProtocolType(QAbstractSocket::NetworkLayerProtocol protocal)
{
    switch(protocal)
    {
    case QAbstractSocket::IPv4Protocol:
        return "IPv4Protocol";
        break;
    case  QAbstractSocket::IPv6Protocol:
        return "IPv6Protocol";
        break;
    case  QAbstractSocket::AnyIPProtocol:
        return "AnyIPProtocol";
        break;
    case  QAbstractSocket::UnknownNetworkLayerProtocol:
        return "UnknownNetworkLayerProtocol";
        break;
    default:
        break;
    }
    return "";
}
private slots:
void Widget::GetIpList(const QHostInfo &host)
{
    QList<QHostAddress> ipaddrList = host.addresses();
    for(int i = 0; i < ipaddrList.size(); i++)
    {
        QHostAddress host = ipaddrList.at(i);
        qDebug() << "协议类型:" << ProtocolType(host.protocol());
        qDebug() << "本地Ip地址:" << host.toString();
    }
}
QString domainName = "www.baidu.com";
QHostInfo::lookupHost(domainName, this, SLOT(GetIpList(QHostInfo)));
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
