前言:Qt是一个1991年由Qt Company开发的跨平台 C++ 图形用户界面利用程序开发框架。它既能够开发GUI程序,也可用于开发非GUI程序,比方控制台工具和服务器。Qt是面向对象的框架,应用非凡的代码生成扩大(称为元对象编译器(MetaObject Compiler, moc))以及一些宏,Qt很容易扩大,并且容许真正地组件编程。
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); }
3、如果创立Actions(可在菜单和工具栏里应用这些Action)
代码演示:
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); newAct->setShortcut(tr("Ctrl+N")); newAct->setStatusTip(tr("Create a new file")); connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); openAct = new QAction(QIcon(":/images/open.png"), tr("&Open"), this); openAct->setShortcut(tr("Ctrl+O")); openAct->setStatusTip(tr("Open an existing file")); connect(openAct, SIGNAL(triggered()), this, SLOT(open())); saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); saveAct->setShortcut(tr("Ctrl+S")); saveAct->setStatusTip(tr("Save the document to disk")); connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); saveAsAct = new QAction(tr("Save &As"), this); saveAsAct->setStatusTip(tr("Save the document under a new name")); connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setStatusTip(tr("Exit the application")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); cutAct->setShortcut(tr("Ctrl+X")); cutAct->setStatusTip(tr("Cut the current selection's contents to the " "clipboard")); connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut())); copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); copyAct->setShortcut(tr("Ctrl+C")); copyAct->setStatusTip(tr("Copy the current selection's contents to the " "clipboard")); connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy())); pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); pasteAct->setShortcut(tr("Ctrl+V")); pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " "selection")); connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste())); aboutAct = new QAction(tr("&About"), this); aboutAct->setStatusTip(tr("Show the application's About box")); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); aboutQtAct = new QAction(tr("About &Qt"), this); aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
4、如果创立主菜单
答:采纳下面的QAction的帮忙,创立主菜单
代码演示:
fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct); fileMenu->addAction(openAct); fileMenu->addAction(saveAct); fileMenu->addAction(saveAsAct); fileMenu->addSeparator(); fileMenu->addAction(exitAct); editMenu = menuBar()->addMenu(tr("&Edit")); editMenu->addAction(cutAct); editMenu->addAction(copyAct); editMenu->addAction(pasteAct); menuBar()->addSeparator(); helpMenu = menuBar()->addMenu(tr("&Help")); helpMenu->addAction(aboutAct); helpMenu->addAction(aboutQtAct);
5、如果创立工具栏
答:采纳下面的QAction的帮忙,创立工具栏
代码演示:
fileToolBar = addToolBar(tr("File")); fileToolBar->addAction(newAct); fileToolBar->addAction(openAct); fileToolBar->addAction(saveAct); editToolBar = addToolBar(tr("Edit")); editToolBar->addAction(cutAct); editToolBar->addAction(copyAct); editToolBar->addAction(pasteAct);
6、如何应用配置文件保留配置
答:应用QSettings类
代码演示:
QSettings settings("Trolltech", "Application Example"); QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); QSize size = settings.value("size", QSize(400, 400)).toSize(); QSettings settings("Trolltech", "Application Example"); settings.setValue("pos", pos()); settings.setValue("size", size());
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::Default, QMessageBox::No, QMessageBox::Cancel | QMessageBox::Escape); if (ret == QMessageBox::Yes) return save(); else if (ret == QMessageBox::Cancel) return false;
8、如何使通用对话框中文化
答:对话框的中文化
比 如说,QColorDialog的与文字相干的局部,次要在qcolordialog.cpp文件中,咱们能够从qcolordialog.cpp用 lupdate生成一个ts文件,而后用自定义这个ts文件的翻译,再用lrelease生成一个.qm文件,当然了,主程序就要扭转要反对多国语言了, 应用这个.qm文件就能够了。
另外,还有一个更快的办法,在源代码解开后有一个目录translations,上面有一些.ts, .qm文件,咱们拷贝一个:
代码演示:
cp src/translations/qt_untranslated.ts ./qt_zh_CN.ts
然 后,咱们就用Linguist关上这个qt_zh_CN.ts,进行翻译了,翻译实现后,保留后,再用lrelease命令生成qt_zh_CN.qm, 这样,咱们把它退出到咱们的qt project中,那些零碎的对话框,菜单等等其它的默认是英文的货色就能显示成中文了。
9、在Windows下Qt里为什么没有终端输入?
答:把上面的配置项退出到.pro文件中
代码演示:
win32:CONFIG += console
10、Qt 4 for X11 OpenSource版如何动态链接?
答:编译装置的时候加上-static选项
代码演示:
/configure -static //肯定要加static选项gmakegmake install
而后,在Makefile文件中加 static 选项或者在.pro文件中加上QMAKE_LFLAGS += -static,就能够连贯动态库了。
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函数中退出上面语句:
’ 代码演示:
TEMPLATE=lib 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.s
14、如何启动一个内部程序
答:可应用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();
15、如何打印报表
答: Qt目前对报表打印反对的库还很少,不过有种变通的办法,就是应用XML+XSLT+XSL-FO来进行报表设计,XML输入数据,用XSLT将XML数 据转换为XSL-FO格局的报表,因为当初的浏览器不间接反对XSL-FO格局的显示,所以临时可用工具(Apache FOP, Java做的)将XSL-FO转换为PDF文档来进行打印,转换和打印由FOP来做,生成XSL-FO格局的报表能够由Qt来生成,也能够由其它内容转换 过去,比方有工具(html2fo)将HTML转换为XSL-FO。
16、如何在系统托盘区显示图标
答:在4.2及其以上版本中应用QSystemTrayIcon类来实现
这是我刷的面试题能够分享, 点击这里
祝大家早点找到本人心仪的工作!!!!!!