我想开网站,用flash做的网站展示,设计开发控制程序,爱企查在线查询先通过ui界面实现基本框架 接下来就要实现每个按键的功能了
我们先来实现新建的的功能#xff0c;我们右键新建键#xff0c;可以发现没有转到槽的功能#xff0c;因此我们要自己写connect来建立关系。 private slots:void newActionSlot();
在.h文件中加上槽函数。 conne…先通过ui界面实现基本框架 接下来就要实现每个按键的功能了
我们先来实现新建的的功能我们右键新建键可以发现没有转到槽的功能因此我们要自己写connect来建立关系。 private slots:void newActionSlot();
在.h文件中加上槽函数。 connect(ui-action_N,QAction::triggered,this,MainWindow::newActionSlot);
然后再.cpp的构造函数中加上connect函数QAction::triggered就是被单击的信号。
建立联系之后我们就可以实现newActionSlot()这个函数。
void MainWindow::newActionSlot()
{ui-textEdit-clear();this-setWindowTitle(新建文本文档.txt);
}
我们使用了clear清空原本的内容然后重新给窗口名取别名。这样一来新建键的功能实现了。
接下来实现打开键。
void openActionSlot(); 老样子在.h文件中加入此函数。
然后再.cpp中建立联系 connect(ui-action_O,QAction::triggered,this,MainWindow::openActionSlot);
实现void openActionSlot()
void MainWindow::openActionSlot()
{QString filename QFileDialog::getOpenFileName(this, 选择一个文件,QCoreApplication::applicationFilePath(),//获取当前路径*.cpp);if(filename.isEmpty()){QMessageBox::warning(this,警告,请选择一个文件);}else{//qDebug()filename;//绝对路径QFile file(filename);file.open(QIODevice::ReadOnly);QByteArray bafile.readAll();ui-textEdit-setText(QString(ba));file.close();}
} 我们来一步一步解析。
QString filename QFileDialog::getOpenFileName(this, 选择一个文件, QCoreApplication::applicationFilePath(),//获取当前路径 *.cpp);
这一行可以获取选择文件的绝对路径。
if(filename.isEmpty()) { QMessageBox::warning(this,警告,请选择一个文件); }
如果没有选择文件回弹出窗口警告加头文件QMessage。 file.open(QIODevice::ReadOnly);以只读的方式打开文件
接下来我们实现另存为键 void saveActionSlot(); connect(ui-action_S,QAction::triggered,this,MainWindow::saveActionSlot);
void MainWindow::saveActionSlot()
{QString filenameQFileDialog::getSaveFileName(this,选择一个文件,QCoreApplication::applicationFilePath());if(filename.isEmpty()){QMessageBox::warning(this,警告,请选择一个文件);}else{QFile file(filename);file.open(QIODevice::WriteOnly);// QByteArray ba;// ba.append(ui-textEdit-toPlainText());file.write(ui-textEdit-toPlainText().toUtf8().data());file.close();}
}
实现这个函数和上述步骤基本一致。
通过以上操作我们实现了基本功能让我们一起来看看效果
点击打开键 没有选择文件 打开文件后成功在textedit中显示 点击另存为键