一步步学Qt,第四天-Qt 建立MainWindow遇到的问题,也是常见GUI工具的问题

一步步学Qt,第四天-Qt 建立MainWindow遇到的问题,也是常见GUI工具的问题



今天写了一个MainWidow,但是遇到的问题,很有意思,先看效果吧:(期望的效果)

结果我的结果是:

也就是,资源文件没有加载.

就在此刻,我突然的冒出来,是不是因为图片格式的问题?我里面改了一个试了试,此时的结果是



也就是说和资源的文件格式有关系,再试试试其他的部分,运行结果:

正常了.在窗口中使用的资源在窗口中的呈现结果,和资源文件的文件格式有关系的.
之前使用的文件是ico文件,那么这个或许在那个时候是可以的,不过因为不稳定所以就不行了.
附上源代码:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void loadFile(QString filename);
    int fileChanged();
    void saveFile();
private:
    Ui::MainWindow *ui;
    QString fileName;
private slots:
    void about();
    void newFile();
    void openFile();
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QTextStream>
#include <QTextEdit>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   setCentralWidget(ui->textEdit);
    //action setting
    ui->mainToolBar->addAction(ui->action_New);
    ui->mainToolBar->addAction(ui->action_Open);
    ui->mainToolBar->addAction(ui->action_Cut);
    ui->mainToolBar->addAction(ui->action_About);
    //slot setting
    connect(ui->action_About,SIGNAL(triggered()),this,SLOT(about()));
    connect(ui->action_New,SIGNAL(triggered()),this,SLOT(newFile()));
    connect(ui->action_Open,SIGNAL(triggered()),this,SLOT(openFile()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::about()
{
    QMessageBox::about(this,tr("About this app"),tr("copyrigth by PowerLinux"));
    return;
}

void MainWindow::newFile()
{
    int i = fileChanged();
    if(i == 1){
        int s = QMessageBox::question(this,tr("Asking"),tr("File is changed ,Save it?"),QMessageBox::Ok | QMessageBox::Cancel);
        if(s == QMessageBox::Ok){
            saveFile();
        }
        else{
            ui->textEdit->setText("");
        }
    }
    else{
            MainWindow *newWin = new MainWindow;
                newWin->show();
    }


}

void MainWindow::openFile()
{
    int i = fileChanged();
    if(i == 1){
        int s = QMessageBox::question(this,tr("Asking"),tr("File is changed ,are you sure?"),QMessageBox::Ok | QMessageBox::Cancel);
        if(s == QMessageBox::Ok){
            fileName = QFileDialog::getOpenFileName(this);
            if (!fileName.isEmpty()) {
                loadFile(fileName);
            }
        }
    }
    else{
        fileName = QFileDialog::getOpenFileName(this);
        if (!fileName.isEmpty()) {
            loadFile(fileName);
        }
    }
}

void MainWindow::loadFile(QString filename){
    QFile file(filename);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
        QTextStream textStream(&file);
        while(!textStream.atEnd()){
            ui->textEdit->append(textStream.readLine());
        }
    }
}

int MainWindow::fileChanged(){
    if(ui->textEdit->document()->isModified()){
       return 1;
    }
    else{
        return 0;
    }
}
void MainWindow::saveFile()
//! [44] //! [45]
{
    fileName = QFileDialog::getSaveFileName(this);
    if (!fileName.isEmpty()){
        QFile file(fileName);
        if (!file.open(QFile::WriteOnly | QFile::Text)) {
            QMessageBox::warning(this, tr("Application"),
                                 tr("Cannot write file %1:\n%2.")
                                 .arg(fileName)
                                 .arg(file.errorString()));
        }
        else{
            QTextStream out(&file);
            out << ui->textEdit->toPlainText();
        }
    }
}

<RCC>
    <qresource prefix="/images">
        <file>images/accessories-text-editor.png</file>
        <file>images/help-browser.png</file>
        <file>images/internet-mail.png</file>
        <file>images/office-calendar.png</file>
    </qresource>
</RCC>

#-------------------------------------------------
#
# Project created by QtCreator 2011-08-27T13:54:14
#
#-------------------------------------------------

QT       += core gui

TARGET = widget
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

RESOURCES += \
    app.qrc

OTHER_FILES += \
    app.rc
RC_FILE +=\
    app.rc




posted @ 2011-08-27 22:30  Podevor  阅读(791)  评论(0编辑  收藏  举报