我在学习qt学习之路第十七章Qt学习之路第17篇 标准对话框 文件对话框,下面链接这节时遇到了这个报错。
error C2143: 语法错误 : 缺少“;”(在“*”的前面) error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
查找很多说是什么头文件重复包含之类的。
http://www.qter.org/thread-638-1-1.html
各种调试尝试后,将mainwindows.cpp中包含的头文件#include
上代码
///这是mainwindows.cpp////////////////////
#include "mainwindows.h"
#include
#include
#include
//#include
#include
#include
#include
mainWindows::mainWindows(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
setWindowTitle(tr("main Window"));
openAction = new QAction(QIcon(":mainWindows/Resources/open.png"),tr("&open..."),this);
openAction -> setShortcuts(QKeySequence::Open);
openAction -> setStatusTip(tr("open an exiting file"));
saveAction = new QAction(QIcon(":mainWindows/Resources/save.png"),tr("&save..."),this);
saveAction -> setShortcuts(QKeySequence::Save);
saveAction -> setStatusTip(tr("Save a new file"));
QMenu *file = menuBar()->addMenu(tr("&File"));
file->addAction(openAction);
file->addAction(saveAction);
QToolBar *toolBar=addToolBar(tr("&File"));
toolBar->addAction(openAction);
toolBar->addAction(saveAction);
//QToolBar *toolBar2 = addToolBar(tr("Tool Bar 2"));
//toolBar2->addAction(openAction);
textEdit = new QTextEdit(this);
setCentralWidget(textEdit);
//connect(openAction,SIGNAL(triggered()),this,SLOT(open()));
connect(openAction,SIGNAL(triggered()),this,SLOT(open()));
connect(saveAction,SIGNAL(triggered()),this,SLOT(save()));
statusBar();
}
mainWindows::~mainWindows()
{
}
void mainWindows::open()
{
QString path = QFileDialog::getOpenFileName(this,
tr("Open File"),
".",
tr("Text Files(*.txt)"));
if(!path.isEmpty()){
QFile file(path);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
QMessageBox::warning(this,tr("Read File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream in(&file);
textEdit->setText(in.readAll());
file.close();
}else{
QMessageBox::warning(this,tr("Path"),
tr("You did not select any file."));
}
}
void mainWindows::save()
{
QString path = QFileDialog::getSaveFileName(this,
tr("Open File"),
".",
tr("Text Files(*.txt)"));
if(!path.isEmpty()){
QFile file(path);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
QMessageBox::warning(this,tr("Write File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream out(&file);
out << textEdit->toPlainText();
file.close();
}else{
QMessageBox::warning(this,tr("Path"),
tr("You did not select any file."));
}
}
/////这是mainwindows.h/////////////
#ifndef MAINWINDOWS_H
#define MAINWINDOWS_H
#include
#include "ui_mainwindows.h"
#include//放到mainwindows.cpp中就报错
class mainWindows : public QMainWindow
{
Q_OBJECT
public:
mainWindows(QWidget *parent = 0, Qt::WFlags flags = 0);
~mainWindows();
private:
Ui::mainWindowsClass ui;
private slots:
void open();
void save();
private:
QTextEdit *textEdit;
QAction *openAction;
QAction *saveAction;
};
#endif // MAINWINDOWS_H
报错截图如下:
错误停留在这个QTextEdit上,如下图
虽然问题解决了,希望qt大神看到后能给我讲讲为什么会这样,是哪里出现了重复调用?能细致解答一下吗?谢谢啦。
PS:顺便说一下,我这里是把open.png和save.png都放在我自己的工程resources资源下了,然后指定这里的路径。添加了保存save后,也没有出现图标,后来又调用了qrc,双击打开添加一下save图标路径,然后就可以显示了。如果又出现qrc打开是代码的情况,就在qrc文件上右键-》打开方式-》添加的路径是D:\Program Files (x86)\Digia\Qt5VSAddin路径下的q5rceditor。类似这个路径,看你自己放哪里了。
这个小例子就成功啦。