Qt::QLableTest操作
前言:
本文操作均为在vs2015+QT5.9.5版本中执行
本文重要部分:
//创建一个QLabel并且播放gif图片
QLabel *lab2 = new QLabel();
//设置窗口位置以及大小
lab2->setGeometry(350.5, 301.5, 319, 197);
//设置背景颜色
lab2->setStyleSheet(“background-color: rgb(170, 255, 255);”);
//添加头文件#include
QMovie *mov = new QMovie(“tree.gif”);
//设置gif
lab2->setMovie(mov);
//设置自动扩展缩放
lab2->setScaledContents(true);
//设置一个窗口
lab2->show();
//开始播放
mov->start();
//设置打开外部链接
ui.label->setOpenExternalLinks(true);
//自定义函数部分
void Hov(QString url)
{
qDebug() << “Hov:” << url<<endl;
qDebug()<< QString::fromLocal8Bit(“移动到位置后获取的链接:” )<< ui.label_2->selectedText() << endl;
}
void Act(QString url)
{
qDebug() << “Act:” << url << endl;
//qDebug() << ui.label_2->text() << endl;
qDebug() <<QString::fromLocal8Bit(“点击后获取的链接:”)<< ui.label_2->selectedText() << endl;
}
头文件:qlabletext.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_qlabletext.h"
#include <QDebug>
class QLableText : public QWidget
{
Q_OBJECT
public:
QLableText(QWidget *parent = Q_NULLPTR);
public slots:
void Hov(QString url)
{
qDebug() << "Hov:" << url<<endl;
qDebug()<< QString::fromLocal8Bit("移动到位置后获取的链接:" )<< ui.label_2->selectedText() << endl;
}
void Act(QString url)
{
qDebug() << "Act:" << url << endl;
//qDebug() << ui.label_2->text() << endl;
qDebug() <<QString::fromLocal8Bit("点击后获取的链接:")<< ui.label_2->selectedText() << endl;
}
private:
Ui::QLableTextClass ui;
};
UI文件:ui_qlabletext.h
/********************************************************************************
** Form generated from reading UI file 'qlabletext.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_QLABLETEXT_H
#define UI_QLABLETEXT_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_QLableTextClass
{
public:
QLabel *label;
QLabel *label_2;
void setupUi(QWidget *QLableTextClass)
{
if (QLableTextClass->objectName().isEmpty())
QLableTextClass->setObjectName(QStringLiteral("QLableTextClass"));
QLableTextClass->resize(1020, 800);
label = new QLabel(QLableTextClass);
label->setObjectName(QStringLiteral("label"));
label->setGeometry(QRect(140, 80, 571, 281));
label->setStyleSheet(QString::fromUtf8("color: rgb(255, 0, 0);\n"
"\n"
"font: 20pt \"\351\273\221\344\275\223\";\n"
"background-color: rgb(170, 255, 255);"));
label->setTextFormat(Qt::RichText);
label_2 = new QLabel(QLableTextClass);
label_2->setObjectName(QStringLiteral("label_2"));
label_2->setGeometry(QRect(210, 390, 471, 241));
label_2->setStyleSheet(QStringLiteral("background-color: rgb(192, 179, 255);"));
label_2->setTextFormat(Qt::RichText);
label_2->setScaledContents(true);
label_2->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextEditable|Qt::TextEditorInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse);
retranslateUi(QLableTextClass);
QObject::connect(label_2, SIGNAL(linkHovered(QString)), QLableTextClass, SLOT(Hov(QString)));
QObject::connect(label_2, SIGNAL(linkActivated(QString)), QLableTextClass, SLOT(Act(QString)));
QMetaObject::connectSlotsByName(QLableTextClass);
} // setupUi
void retranslateUi(QWidget *QLableTextClass)
{
QLableTextClass->setWindowTitle(QApplication::translate("QLableTextClass", "QLableText", Q_NULLPTR));
label->setText(QApplication::translate("QLableTextClass", "<html><head/><body><p align=\"center\">\350\256\277\351\227\256CSDN\357\274\232</p><p align=\"center\"><a href=\"https://blog.csdn.net/weixin_45357007?spm=1018.2226.3001.5343\"><span style=\" text-decoration: underline; color:#0000ff;\">ciao\346\270\224\345\244\253\344\270\273\351\241\265</span></a></p></body></html>", Q_NULLPTR));
label_2->setText(QApplication::translate("QLableTextClass", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:20pt;\">CSDN\346\230\257\345\205\250\347\220\203\347\237\245\345\220\215\344\270\255\346\226\207IT\346\212\200\346\234\257\344\272\244\346\265\201\345\271\263\345\217\260</span></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><a href=\"www.csdn.net\"><span style=\" font-size:20pt; text-decoration: underline; color:#0000ff;\">www.csdn.net</span></a></p></body></html>", Q_NULLPTR));
} // retranslateUi
};
namespace Ui {
class QLableTextClass: public Ui_QLableTextClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_QLABLETEXT_H
主函数:main.cpp
#include "qlabletext.h"
#include <QtWidgets/QApplication>
#include <QMovie>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/************************************************************************/
//创建一个QLabel并且播放gif图片
QLabel *lab2 = new QLabel();
//设置窗口位置以及大小
lab2->setGeometry(350.5, 301.5, 319, 197);
//设置背景颜色
lab2->setStyleSheet("background-color: rgb(170, 255, 255);");
//添加头文件#include <QMovie>
QMovie *mov = new QMovie("tree.gif");
//设置gif
lab2->setMovie(mov);
//设置自动扩展缩放
lab2->setScaledContents(true);
//设置一个窗口
lab2->show();
//开始播放
mov->start();
/************************************************************************/
QLableText w;
w.show();
return a.exec();
}
实现文件:qlabletext.cpp
#include "qlabletext.h"
#include <QDebug>
#include <QMovie>
QLableText::QLableText(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//QString lable_text = QString::fromLocal8Bit("文本测试\n第二行");
//ui.label->setText(lable_text);
//设置打开外部链接
ui.label->setOpenExternalLinks(true);
}