一、收获
二、代码
jokelabel.h
#include<QLabel>
#include<QToolButton>
class JokeLabel : public QLabel
{
Q_OBJECT
public:
JokeLabel(QWidget *parent = 0);
~JokeLabel();
QString jokeLoader(int n);
QString joke;
};
jokelabel.cpp
#include "jokeLabel.h"
#include<QFile>
#include<QXmlStreamReader>
#include<QTextCodec>
#include<QTextStream>
JokeLabel::JokeLabel(QWidget *parent)
: QLabel(parent)
{
joke=("null");
}
QString JokeLabel::jokeLoader (int n)
{
/*通过QFile打开birhtday.xml,QTextStream读取birthday.xml 通过QTextStream设置编码显示中文,然后通过QXmlStreamReader处理xml文件*/
QFile xmlFile(":/birthday.xml");
if (!xmlFile.exists() || (xmlFile.error() != QFile::NoError)) {
return QString("null");
}
xmlFile.open(QIODevice::ReadOnly);
QTextStream textReader(&xmlFile);
QTextCodec *codec=QTextCodec::codecForName("UTF-8");
textReader.setCodec(codec);//转换显示中文
QString content = textReader.readAll();
xmlFile.close ();
QXmlStreamReader reader(content);
while (!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()&&
reader.name ()=="joke"&&
reader.attributes().value ("id").toString ().toInt()==n) {//获取随机数对应的joke
joke= reader.attributes().value("content").toString ();
break;
}
}
return joke;
}
JokeLabel::~JokeLabel()
{
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QPushButton>
#include<QDialog>
#include "jokeLabel.h"
#include<QWidget>
class JokeLabel;
class MainWindow : public QDialog
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void createPushButton();
void Layout();
signals:
public slots:
void showLabel();
private:
JokeLabel *label;
QPushButton *quitButton;
QPushButton *refreshButton;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include<QHBoxLayout>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent) :
QDialog(parent)
{
/*设置MainWindow的风格:背景图片及其位置*/
setStyleSheet(QString::fromUtf8("background-image: url(:/background);\
background-position:center;"));
label=new JokeLabel;
/*设置label的风格,颜色:黑,无边界,字体大小*/
/*关于stylesheet可以参考qt文档的Qt StyleSheet Reference*/
label->setStyleSheet (tr("color:black;border:none;font:bold normal;\
font-size: 18px"));
createPushButton ();
Layout();
connect(refreshButton,SIGNAL(released()),this,SLOT(showLabel()));
connect(quitButton,SIGNAL(released()),this,SLOT(close()));
}
void MainWindow::showLabel()
{
refreshButton->clearFocus ();
int n=std::rand()%67;//产生随机数
label->setText(label->jokeLoader (n));
label->setGeometry(QRect(0, 0, 480, 800));
label->setWordWrap(true);//text包裹在label中,实现自动换行
label->setAlignment(Qt::AlignTop);//文字对齐
label->show ();
}
void MainWindow::Layout ()
{
QGridLayout *main1Layout=new QGridLayout;
main1Layout->addWidget (quitButton,31,25,5,5);
main1Layout->addWidget (refreshButton,31,0,5,5);
main1Layout->addWidget (label,0,0,30,30);
setLayout(main1Layout);
}
MainWindow::~MainWindow ()
{
delete label;
delete quitButton;
delete refreshButton;
}
void MainWindow::createPushButton ()
{
quitButton =new QPushButton(tr("退 出"),this);
quitButton->setStyleSheet
(QString::fromUtf8\
("background-image: url(:/toolbutton);\
background-position:center;\
border:none;color:white;\
font: normal;\
font-size: 20px"));
quitButton->setFixedSize (65,44);
quitButton->clearFocus ();
refreshButton =new QPushButton(this);
refreshButton->setStyleSheet
(QString::fromUtf8\
("background-image: url(:/toolbutton);\
background-position: center;\
border:none;color:white;\
font: normal ;\
font-size: 20px;"));
refreshButton->setText (tr("刷 新"));
refreshButton->setFixedSize (65,44);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include<QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));//按钮显示中文不乱码
MainWindow w;
w.show();
return a.exec();
}