原文:http://www.cnblogs.com/feiyangqi ... /12/06/1898143.html
1、如果在窗体关闭前自行判断是否可关闭
答:重新实现这个窗体的 closeEvent()函数,加入判断操作
void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave())
{
writeSettings();
event->accept();
}
else
{
event->ignore();
}
}
2、如何用打开和保存文件对话框
答:使用QFileDialog
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
{
loadFile(fileName);
}
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())
{
return false;
}
如果用qt自带的话: 选择文件夹 QFileDialog* openFilePath = new QFileDialog( this, " 请选择文件夹", "file"); //打开一个目录选择对话框
openFilePath-> setFileMode( QFileDialog::bigirectoryOnly );
if ( openFilePath->exec() == QDialog::Accepted )
{
//code here!
}
delete openFilePath; 选择文件: QFileDialog *openFilePath = new QFileDialog(this);
openFilePath->setWindowTitle(tr("请选择文件"));
openFilePath->setDirectory(".");
openFilePath->setFilter(tr("txt or image(*.jpg *.png *.bmp *.tiff *.jpeg *.txt)"));
if(openFilePath->exec() == QDialog::Accepted)
{
//code here
}
delete openFilePath;
7、如何使用警 告、信息等对话框
答:使用QMessageBox类的静态方法
int ret = QMessageBox::warning(this, tr("Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::bigefault,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
if (ret == QMessageBox::Yes)
return save();
else if (ret == QMessageBox::Cancel)
return false;
或者简单点儿: QMessageBox::information(this, "关于","盲人辅助系统(管理端)!\nVersion:1.0\nNo Copyright");
9、在Windows下Qt里为什么没有终端输出?
答:把下面的配置项加入到.pro文件中
win32:CONFIG += console
11、想在源代码中直接使用中文,而不使用tr()函数进行转换,怎么办?
答:在main函数中加入下面三条语句,但并不提倡
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
或者 QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
使用GBK还是使用UTF-8,依源文件中汉字使用的内码而定
这样,就可在源文件中直接使用中文,比如:
QMessageBox::information(NULL, "信息", "关于本软件的演示信息", QMessageBox::Ok, QMessageBox::NoButtons);
12、为什么将开发的使用数据库的程序发布到其它机器就连接不上数据库?
答:这是由于程序找不到数据库插件而致,可照如下解决方 法:
在main函数中加入下面语句:
QApplication::addLibraryPath(strPluginsPath");
strPluginsPath是插件所在目录,比如此目录为/myapplication/plugins
则将需要的sql驱 动,比如qsqlmysql.dll, qsqlodbc.dll或对应的.so文件放到
/myapplication/plugins/sqldrivers/
目 录下面就行了
这是一种解决方法,还有一种通用的解决方法,即在可执行文件目录下写qt.conf文件,把系统相关的一些目录配置写到 qt.conf文件里,详细情况情参考Qt Document Reference里的qt.conf部分
13、如何创建QT使 用的DLL(.so)以及如何使用此DLL(.so)
答:创建DLL时其工程使用lib模板
TEMPLATE=lib
而源文件则和使用普通的源文件一样,注意把头文件和源文件分开,因为在其它程序使用此DLL时需要此头文件
在使用此DLL时,则 在此工程源文件中引入DLL头文件,并在.pro文件中加入下面配置项:
LIBS += -Lyourdlllibpath -lyourdlllibname
Windows下和Linux下同样(Windows下生成的DLL文件名为yourdlllibname.dll而在Linux下生成 的为libyourdlllibname.so。注意,关于DLL程序的写法,遵从各平台级编译器所定的规则。
14、如何启动一个外部程 序
答:1、使用QProcess::startDetached()方法,启动外部程序后立即返回;
2、使用 QProcess::execute(),不过使用此方法时程序会最阻塞直到此方法执行的程序结束后返回,这时候可使用QProcess和QThread 这两个类结合使用的方法来处理,以防止在主线程中调用而导致阻塞的情况
先从QThread继承一个类,重新实现run()函数:
class MyThread : public QThread
{
public:
void run();
};
void MyThread::run()
{
QProcess::execute("notepad.exe");
}
这样,在使用的时候则可定义一个MyThread类型的成员变量,使用时调用其start()方法:
class ...............
{...........
MyThread thread;
............
};
.....................
thread.start();
|