Qt学习 第2节:QLabel

本文介绍了如何在Qt中使用QLabel创建带有HTML标签的文本,并将其融入到一个继承自QMainWindow的窗口类中。展示了如何实例化 QLabel 并设置其样式,以及如何将这个标签添加到主窗口中显示。
#include <QApplication>
#include <QLabel.h>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QLabel* mylabel= new QLabel("<h1><i>Hello</i><font color=red>Qt!</font></h1>");

    mylabel->show();

    return app.exec();
}

工程构成

一个继承自 QMainWindow 的主窗口类,并起名为 Window,该类的定义文件为 Window.h 头文件中,实现文件为 Window.cpp 源文件中。
#ifndef WINDOW_H
#define WINDOW_H

#include <QMainWindow>
#include <QLabel>   // 引入 QLable 标签组件的头文件

class Window : public QMainWindow
{
    // QMainWindow 是主窗口类,主窗口具有主菜单栏、工具栏和状态栏,类似于一般的应用程序的主窗口;
    // QWidget 是所有具有可视界面类的基类,选择 QWidget 创建的界面对各种界面组件都可以 支持;
    // QDialog 是对话框类,可建立一个基于对话框的界面;
    Q_OBJECT
    // 一个已定义好的宏,所有需要“信号和槽”功能的组件都必须将 Q_OBJECT 作为 private 属性成员引入到类中

public:
    Window(QWidget *parent = 0);
    ~Window();
private:
    QLabel* label;  // 定义一个私有的 QLabel 指针对象
};

#endif // WINDOW_H
#include "Window.h"

Window::Window(QWidget *parent) : QMainWindow(parent)
{
    this->label = new QLabel("Hello Qt !",this);//this指定当前空间的父窗口Window
    // 	QLabel的构造函数QLabel(const QString & text, QWidget * parent = 0, Qt::WindowFlags f = 0)
}

Window::~Window()
{

}

主函数main

#include "Window.h"
#include <QApplication>
// main() 函数中分别定义了 QApplication 和 Window 类的对象,需要引入 QApplication 和 Windows.h 头文件。
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    Window w;
    w.show();   // Qt 提供的所有组件(控件、部件)都是隐藏的,不会自动显示。通过调用 Window 类提供的 show() 方法,w 窗口就可以在程序运行后显示出来。

    return app.exec();
}

0.QLabel<--QFrame<--QWidget<--QObject and QPaintDevice

QLabel: provides a text or image display
QFrame: the base class of widgets that can have a frame
QWidget: the base class of all user interface objects
QObject: the base class of all Qt objects
QPaintDevice: the base class of objects that can be painted on with QPainter

0.QPushButton<--QAbstractButton<--QWidget<--QObject and QPaintDevice

QPushButton: provides a command button
QAbstractButton: the abstract base class of button widgets, providing functionality common to buttons
QWidget: the base class of all user interface objects
QObject: the base class of all Qt objects
QPaintDevice: the base class of objects that can be painted on with QPainter

1.QMainWindow<--QWidget<--QObject and QPaintDevice

QMainWindow: provides a main application window
QWidget: the base class of all user interface objects
QObject: the base class of all Qt objects
QPaintDevice: the base class of objects that can be painted on with QPainter

1.QDialog<--QWidget<--QObject and QPaintDevice

QDialog: the base class of dialog windows
QWidget: the base class of all user interface objects
QObject: the base class of all Qt objects
QPaintDevice: the base class of objects that can be painted on with QPainter

2.QApplication<--QGuiApplication<--QCoreApplication<--QObject

QApplication: manages the GUI application's control flow and main settings
QGuiApplication: manages the GUI application's control flow and main settings
QCoreApplication: provides an event loop for Qt applications without UI
QObject: the base class of all Qt objects

3.QLayout<--QObject and QLayoutItem

QLayout: the base class of geometry managers
QObject: the base class of all Qt objects
QLayoutItem: provides an abstract item that a QLayout manipulates

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值