//#include "widget.h"
#include <QApplication>
#include <QMessageBox>
#include <QDebug>
#include <QTextCodec>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QString str = QStringLiteral("中文测试");
qDebug()<<str;
QMessageBox::information(0,"中文测试","中文测试");
//元数据是gbk或者gb2312 多字节存入QString
//本地编码方式
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
char *str1 = "元数据中文gbk";
qDebug()<<"str1 = "<<str1;
QString str2 = QString::fromLocal8Bit(str1);
qDebug()<<"str2 = "<<str2;
//把QString 转为gbk
cout<<str2.toLocal8Bit().toStdString()<<endl;
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QString str3 = QString::fromLocal8Bit(str2.toUtf8());
qDebug()<<"str3 = "<<str3;
return app.exec();
}
QLabel
1.QLabel 用于显示文本或图像,不提供用户交互功能。
2.样式设置字体,颜色,背景色
3.显示图片
4.播放动画
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QLabel *label;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
label = new QLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(180, 120, 421, 201));
label->setStyleSheet(QString::fromUtf8("color: rgb(164, 0, 0);\n"
"font: 700 italic 50pt \"Ubuntu Condensed\";\n"
"background-color: rgb(52, 101, 164);"));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
label->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QLabel *label;
QLabel *label_2;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
label = new QLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(180, 120, 421, 201));
label->setStyleSheet(QString::fromUtf8("color: rgb(164, 0, 0);\n"
"font: 700 italic 50pt \"Ubuntu Condensed\";\n"
"background-color: rgb(52, 101, 164);"));
label_2 = new QLabel(Widget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(140, 360, 421, 181));
label_2->setStyleSheet(QString::fromUtf8(""));
label_2->setPixmap(QPixmap(QString::fromUtf8(":/test.gif")));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
label->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
label_2->setText(QString());
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
动画
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QString str = QStringLiteral("中文\ndsada");
ui->label->setText(str);
QLabel *lab2 = new QLabel(this);
lab2->setGeometry(0,0,300,300);
lab2->setStyleSheet("background-color: rgb(52, 101, 164)");
QMovie *mov = new QMovie("test.gif");
lab2->setMovie(mov);
lab2->show();
mov->start();
}
富文本
QLabel 链接事件
void linkActivated(const QString &link)
void linkHovered(const QString &link)
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QLabel *label;
QLabel *label_2;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
label = new QLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(60, 340, 461, 241));
label->setStyleSheet(QString::fromUtf8("color: rgb(164, 0, 0);\n"
"font: 700 italic 50pt \"Ubuntu Condensed\";\n"
"border-image: url(:/test.gif);\n"
"background-color: rgb(52, 101, 164);"));
label->setTextFormat(Qt::RichText);
label_2 = new QLabel(Widget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(540, 320, 461, 241));
label_2->setStyleSheet(QString::fromUtf8("color: rgb(164, 0, 0);\n"
"font: 700 italic 50pt \"Ubuntu Condensed\";\n"
"border-image: url(:/test.gif);\n"
"background-color: rgb(52, 101, 164);"));
label_2->setTextFormat(Qt::RichText);
retranslateUi(Widget);
QObject::connect(label_2, SIGNAL(linkActivated(QString)), Widget, SLOT(Act(QString)));
QObject::connect(label_2, SIGNAL(linkHovered(QString)), Widget, SLOT(Hov(QString)));
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
label->setText(QCoreApplication::translate("Widget", "<html><head/><body><p><a href=\"www.test.com\"><span style=\" text-decoration: underline; color:#0000ff;\">ww.test.com</span></a><br/></p></body></html>", nullptr));
label_2->setText(QCoreApplication::translate("Widget", "<html><head/><body><p><a href=\"www.test.com\"><span style=\" text-decoration: underline; color:#0000ff;\">ww.test.com</span></a><br/></p></body></html>", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void Act(QString url)
{
qDebug()<<"click "<<url;
}
void Hov(QString url)
{
qDebug()<<"Hover "<<url;
}
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
QLabel选择和编辑
1.setTextInteractionFlags
2.selectedText
3.setSelection
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//QString str = QStringLiteral("中文\ndsada");
//ui->label->setText(str);
//设置QLabel选择,2选择开始位置,4选址几个字
ui->label->setSelection(2,4);
QLabel *lab2 = new QLabel(this);
lab2->setGeometry(0,0,300,300);
lab2->setStyleSheet("background-color: rgb(52, 101, 164)");
QMovie *mov = new QMovie("test.gif");
lab2->setMovie(mov);
lab2->show();
mov->start();
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include "ui_widget.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void Act(QString url)
{
qDebug()<<"Act "<<url;
}
void SelectHov(QString url)
{
qDebug()<<ui->label->text();
qDebug()<<ui->label->selectedText();
}
void Hov(QString url)
{
qDebug()<<"Hover "<<url;
}
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QLabel *label;
QLabel *label_2;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
label = new QLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(60, 340, 461, 241));
label->setStyleSheet(QString::fromUtf8("color: rgb(164, 0, 0);\n"
"font: 700 italic 50pt \"Ubuntu Condensed\";\n"
"border-image: url(:/test.gif);\n"
"background-color: rgb(52, 101, 164);"));
label->setTextFormat(Qt::RichText);
label->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextEditable|Qt::TextEditorInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse);
label_2 = new QLabel(Widget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(540, 320, 461, 241));
label_2->setStyleSheet(QString::fromUtf8("color: rgb(164, 0, 0);\n"
"font: 700 italic 50pt \"Ubuntu Condensed\";\n"
"border-image: url(:/test.gif);\n"
"background-color: rgb(52, 101, 164);"));
label_2->setTextFormat(Qt::RichText);
retranslateUi(Widget);
QObject::connect(label_2, SIGNAL(linkActivated(QString)), Widget, SLOT(Act(QString)));
QObject::connect(label_2, SIGNAL(linkHovered(QString)), Widget, SLOT(Hov(QString)));
QObject::connect(label, SIGNAL(linkHovered(QString)), Widget, SLOT(SelectHov(QString)));
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
label->setText(QCoreApplication::translate("Widget", "<html><head/><body><p><a href=\"www.test.com\"><span style=\" text-decoration: underline; color:#0000ff;\">ww.test.com</span></a><br/></p></body></html>", nullptr));
label_2->setText(QCoreApplication::translate("Widget", "<html><head/><body><p><a href=\"www.test.com\"><span style=\" text-decoration: underline; color:#0000ff;\">ww.test.com</span></a><br/></p></body></html>", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H