1.在窗口输出Hello BM
新建项目,QT Widget Application
//mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QLabel>
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
//创建一个QLabel控件
QLabel *label=new QLabel(this);
//QLable控件显示文字内容
label->setText("hello BM");
//显示位置
label->setGeometry(QRect(50,50,200,25));
//QRect(在窗体中X轴参数,在窗体中Y轴参数,QLabel控件宽度,QLabel控件高度)
this->setWindowTitle("Qt5.3 窗体应用");
//窗体图标,最好为16*16像素的icOo格式图片。图片如果不起别名,后缀直接写图片全名
this->setWindowIcon(QIcon(":/new/prefix2/kuaibo"));
//this->setWindowIcon(QIcon(":/new/prefix1/windowico.gif"));
//设置窗体大小不能更改
this->setMaximumSize(300,200);
this->setMinimumSize(300,200);
this->move(100,200);
//移动窗体,默认窗体居中显示,move是与你电脑屏幕的左上角的距离
//背景红色
this->setStyleSheet("background:red");
}
MainWindow::~MainWindow()
{
delete ui;
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}