Qt example之随机笑话

新手学习,练手来玩的,大鸟别笑话了。。

设计思路:随机显示读取保存在xml文件中的一条笑话

                 研究中:显示网络获取的笑话。

一、收获

1.setStyleSheet()

   通过这个api可以轻松设置widget的显示风格包括background,font,text color,border(边界)等属性,具体可以参考Qt docement的

2.编写一个派生类实现自己期望的功能是,要先考虑应该继承那一个基类才是最合适的。

3.通过necessitas Qt 可以方便将一些简单的apps移植到android上。

二、代码

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()
{

}


MainWindow.h
 

#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();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值