美文网首页
Part2:多窗体切换,静态公共方法实现。

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

作者: xqiiitan | 来源:发表于2025-05-12 11:30 被阅读0次

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

相关文章

  • JavaScript模板方法模式

    在静态语言中,实现模板方法模式通过继承实现。通过抽象父类封装子类的算法框架——包括实现一些公共方法以及封装子类中所...

  • iOS静态库之间互调冲突问题

    研究目的 1. 在其他静态库(B静态库)中导入公共静态库(A静态库),想直接使用公共静态库(A静态库)中的方法和m...

  • Android学习笔记08—ActionBar回退按钮标准模式开

    实现描述 在使用android应用时,经常会点击回退按钮,点击后当前窗体会切换到上一个窗体。而有时浏览列表信息时,...

  • es6 class实现静态属性、私有属性、方法

    1.class实现静态属性 参考:ES6 class 静态属性和私有方法 es6中实现了静态方法,但是没有静态属性...

  • 单例设计模式

    1.三私一公 即 私有的静态属性 私有的construct方法 私有的clone方法 公共的静态创建类方法 $ob...

  • template 转载dcloud.io

    template(模板页面) hello mui示例App中无等待窗体切换的实现是基于模板页面,点击一个链接,不显...

  • 页面静态化 -三种方式

    方法一:通过file_get_contents()实现静态化 方法二:获取缓存中的内容实现静态化 方法三:通过cu...

  • Kotlin 对象

    Kotlin 没有静态类的概念, 但是命名对象可以实现静态类,伴随对象可以实现静态方法。 静态类: 伴随对象: 参...

  • winform form之间传递参数

    窗体之间传递参数总结: 方案一: 窗体属于类,可以在窗体类中定义已全局变量,类型为公开、静态的。 示例: publ...

  • 强智科技·教务网成绩爬取系列文章之wxpython·GUI简单设

    秋风萧瑟,提前祝大家七夕快乐!!! GUIManager.py是GUI各个窗体切换和联动的中枢,代码实现比较简单,...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/dpouijtx.html