本文主要是介绍一步步学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_OBJECTpublic: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 settingui->mainToolBar->addAction(ui->action_New);ui->mainToolBar->addAction(ui->action_Open);ui->mainToolBar->addAction(ui->action_Cut);ui->mainToolBar->addAction(ui->action_About);//slot settingconnect(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 guiTARGET = widget
TEMPLATE = appSOURCES += main.cpp\mainwindow.cppHEADERS += mainwindow.hFORMS += mainwindow.uiRESOURCES += \app.qrcOTHER_FILES += \app.rc
RC_FILE +=\app.rc
这篇关于一步步学Qt,第四天-Qt 建立MainWindow遇到的问题,也是常见GUI工具的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!