close
新增一個<空的Qt專案>
輸入名稱:HorzVertLayout
點選專案後按滑鼠右鍵,點選<新增檔案>
輸入名稱main.cpp
可以看到剛才新增的main.cpp已經自動加入HorzVertLayout.pro中SOURCES
======================================================================
編輯main.cpp
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->show();
return app.exec();
}
Compile it! 我們可以得到一個非常陽春的window視窗
插入第五行程式碼
1: int main(int argc, char *argv[])
2: {
3: QApplication app(argc, argv);
4: QWidget *window = new QWidget();
5: window->setWindowTitle("This is a window");
6: window->show();
7: return app.exec();
8: }
compile it!
// 8~17行 加入三個按鈕
1. 創建3個按鈕 pushtn1, pushbtn2, pushbtn3
2. 創建1個水平layout: hlayout
3. 將三個按鈕加入水平layout
4. 將水平layout加入window
1: int main(int argc, char *argv[])
2: {
3: QApplication app(argc, argv);
4: QWidget *window = new QWidget();
5: window->setWindowTitle("This is a window");
6:
7: // 8~17行 加入三個按鈕
8: QPushButton *pushbtn1 = new QPushButton("one");
9: QPushButton *pushbtn2 = new QPushButton("two");
10: QPushButton *pushbtn3 = new QPushButton("three");
11:
12: QHBoxLayout *hlayout = new QHBoxLayout();
13: hlayout->addWidget(pushbtn1);
14: hlayout->addWidget(pushbtn2);
15: hlayout->addWidget(pushbtn3);
16:
17: window->setLayout(hlayout);
18:
19: window->show();
20: return app.exec();
21: }
compile it!
1: QPushButton *pushbtn1 = new QPushButton("one");
2: QPushButton *pushbtn2 = new QPushButton("two");
3: QPushButton *pushbtn3 = new QPushButton("three");
4:
5: //QHBoxLayout *hlayout = new QHBoxLayout();
6: QVBoxLayout *vlayout = new QVBoxLayout();
7:
8: vlayout->addWidget(pushbtn1);
9: vlayout->addWidget(pushbtn2);
10: vlayout->addWidget(pushbtn3);
11:
12: window->setLayout(vlayout);
compile it!
參考來源: C++ Qt 08 - Horizontal and Vertical Layouts
source code: HorzVertLayout
全站熱搜
留言列表