Part2:多窗体切换,静态公共方法实现。

2025-05-12  本文已影响0人  xqiiitan

1.倒计时效果和动画播放。

#include <QTimer> // QMovie,QDebug

int index =10;
QTimer* myTimer;
private slots:
    void doProcessTimeout();

myTimer = new QTimer(this); //倒计时
connect(myTimer,SIGNAL(timeout()), 
    this, SLOT(doProcessTimeout()));
myTimer->start();    // 开始倒计时
//播放动画,播放
QMovie *movie = new Movie(this);
movie->setFileName("../demo/boom.gif")
ui->label->setMovie(movie);
ui->label->setScaledContents(true);

doProcessTimeout(){
    index --;
    if(index ==0) {
        myTimer->stop();
        // 播放动画
        movie.start();
        ui->label->show();
        ui->lcdNum->hide();
        this->showFullScreen();
    }
    ui->lcdNum->display(index);
    qDebug() << index << endl;
}

2.消息对话框 QMessageBox

// 带Ok键
QMessageBox::about(this,"关于标题", "内容:开发者:Tom");
QMessageBox::StandardButton ret = QMessageBox::information(this, "title","text", 
             QMessageBox::Yes|QMessageBox::No|QMessageBox::Help, //显示几个按键 
             QMessageBox::Yes); //聚焦在Yes上。
if(ret == QMessageBox::No) {
    qDebug() << "点选了对话框的No键" << endl;
}else if(ret == QMessageBox::Yes) {
    qDebug() << "点选了对话框的Yes键" << endl;
}else if(ret == QMessageBox::Help) {
    qDebug() << "点选了对话框的Help键" << endl;
}
//warning
QMessageBox::StandardButton ret = QMessageBox::warning(this, "warning","warning", 
             QMessageBox::Yes|QMessageBox::No|QMessageBox::Help, //显示几个按键 
             QMessageBox::Yes);

//颜色对话框 QColorDialog。
QColor color =QColorDialog::getColor(Qt::green, this, "SelectColor",
    QColorDialog::ShowAlphaChannel);
if(color.isValid()) { // 有效color 
    QPalette palette = ui->lineEdit->palette();
    palette.setColor(QPalette::Text, color);    
    ui->lineEdit->setPalette(palette);
    
    QPalette palette2 = ui->label->palette();
    palette2.setColor(QPalette::WindowText, color); 
    ui->label->setPalette(palette2);
}

3.多窗体切换,静态公共方法实现。

页面oneform,twoform,threeform。
定义三个辅助类 comoneform.h/.cpp, comtwoform.h/.cpp, comthreeform.h/.cpp.

// comoneform.h/.cpp
#include "oneform.h"
class ComOneForm
{
public:
    ComOneForm();
    static oneform *p_oneform; // 静态公共方法
    static void InitForm();
}
//------------------------------
oneform *ComOneForm::p_oneform = NULL;
ComOneForm::ComOneForm(){}
void ComOneForm::InitForm(){
    p_oneform = new oneform(); //初始化,分配空间
}
// comtwoform.h/.cpp , comthree.h/cpp 类似。
#include "twoform.h"
class ComTwoForm
{
public:
    ComTwoForm();
    static twoform *p_twoform; // 静态公共方法
    static void InitForm();
}
//------------------------------
oneform *ComTwoForm::p_twoform = NULL;
ComTwoForm::ComTwoForm(){}
void ComTwoForm::InitForm(){
    p_twoform = new twoform(); //初始化 分配空间
}

//main.c

#include <QApplication>
#include "comoneform.h"
#include "comtwoform.h" //页面23只初始化,不显示
#include "comoneform.h"
int main(int argc, char* argv[]) {
    QApplication a(argc,argv);
    ComOneForm::InitForm();
    ComOneForm::p_oneform->show();
    
    ComTwoForm::InitForm();
    ComThreeForm::InitForm();
    return a.exec();
}

// 页面1.

#include "comtwoform.h"
#include "comthreeform.h"
oneform::oneform(QWidget* parent):QWidget(parent),ui(new Ui::oneform)
{
    ui->setupUi(this);
}
// 页面1,点击跳转到页面2.
void oneform::on_btn_go2form_clicked() {
    this->hide();
    ComTwoForm::p_twoform->show(); // 页面2显示。
}
void oneform::on_btn_go3form_clicked() {
    this->hide();
    ComThreeForm::p_threeform->show(); // 页面3显示。
}

// 页面2.

#include "comoneform.h"
#include "comthreeform.h" // 引入公共部分头文件。
#include "ui_twoform.h"
twoform::twoform(QWidget* parent):QWidget(parent),ui(new Ui::twoform)
{
    ui->setupUi(this);
}
// 页面2,点击跳转到页面1.
void twoform::on_btn_go1form_clicked() {
    this->hide();
    ComOneForm::p_oneform->show(); // 页面1显示。
}
void twoform::on_btn_go3form_clicked() {
    this->hide();
    ComThreeForm::p_threeform->show(); // 页面3显示。
}

// 页面3 类似,能跳转到页面1,页面2.
页面能随意切换,

4.字体对话框

bool ok;
QFont myFont = QFontDialog::getFont(&ok, QFont("Ubuntu",12),this,
    "get font", QFontDialog::ScalableFonts);
if(ok) ui->label->setFont(myFont); 

5.QInputDialog 独立窗体。

// 输入框
int i =QInputDialog::getInt(this,"get int","Age:", 18,1,100,1);
qDebug() << i << endl;
// 进度条,最大值最小值。
QProgressDialog progress("copyFile","AbortCopy",0,numFiles,this);
progress.setWindowModality(Qt::WindowModal);
progress.setValue(i); // 更新进度
progress->setRange(0, 100);
connect(p_dialog, SIGNAL(canceled()),
        this, SLOT(doProcessCancel()));

QFileDialog,QFile,文件操作。

QFile *myFile; // 指针
myFile = new QFile(this);

{
    QString fileName =QFileDialog::getOpenFileName(this,"getFile","../demoDialog/",
        "HEADERS(*.h);;FORMS(*.ui);;SOURCES(*.cpp)");
    if(fileName.isEmpty()) {
        //提示用户
        return;
    }
    myFile->setFileName(fileName);
    bool ret = myFile->open(QIODevice::ReadOnly|QIODevice::Text);
    if(!ret){
        // 打开文件失败
        return;
    }
    //读取文件,显示, 读取行,读取所有。
    while(!myFile->atEnd()){
        QByteArray ba = myFile->read(MSG_LEN);
        QString str = QString(ba);
        ui->editText->append(str);
    }
    // QByteArray ba = myFile->readAll();
    
    myFile->close();
}

文本流形式读取。支持大文本文件读取。

QTextStream

QTextStream  text(myFile);
while(!text.atEnd()) {
    QString str= text.readAll();
    ui->editText->append(str);
}

打开文件, 写入数据。

void Widget::on_btn_saveFile_clicked() { // 保存文件
    QString fileName = QFileDialog::getSaveFileName(this,"saveFile",
            "/home", "Text(*.cpp *.h)");
    if(fileName.isEmpty()) {
        // 提示用户
        return;
    }
    myFile->setFileName(fileName);
    bool ret = myFile->open(QIODevice::WriteOnly|QIODevice::Text);
    if(!ret){
        // 打开文件失败
        return;
    }
    // QFile直接写入。
    //QString input = ui->textEdit->toPlainText();
    //qint64 len = myFile->write(input.toLocal8Bit());
    //myFile->close();
    
    // 文本流形式写入。
    QTextStream stream(myFile); 
    QString input = ui->textEdit->toPlainText();
    stream << str;
    stream.flush(); 
    myFile->close();
}

错误消息对话框。

QErrorMessage

上一篇 下一篇

猜你喜欢

热点阅读