I try to create "Save as..." dialog in Ubuntu. But I want to use it just for save as a file is there any way to pass file and its path to this dialog?
this is my code:
int main(int agc,char **argv){
QApplication app(argc,argv);
QFileDialog my;
my.getSaveFileName(0."Save file ",QDir::currentPath,"Music files(*.mp3;;Text files (*.txt)"));
my.selectFile("myfile.txt");
return 0;
}
You should pass the file name and its path in the second argument.
Example:
#include <QApplication>
#include <QFileDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString path = QDir::currentPath() + "/myfile.txt";
QString fileName = QFileDialog::getSaveFileName(0, "Save file",
path,
"Music files(*.mp3;;Text files (*.txt)");
return a.exec();
}
Related
I want to create a PDF with Qt. This is my code:
#include <QPdfWriter>
#include <QTextDocument>
int main() {
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
It crashes on the last line with
QPaintDevice: Cannot destroy paint device that is being painted
What did I do wrong?
You didn't create a QApplication. Change the code to
#include <QApplication>
#include <QPdfWriter>
#include <QTextDocument>
int main(int argc, char *argv[]) {
QApplication app{argc, argv};
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
and it will create the PDF. Running app.exec() is not required.
I can't add a database to resources:
#include "QtSql/QSqlDatabase"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":/new/prefix1/database.db");
if (!db.open())
qDebug()<<"ERRR"; // this line is printed at startup
return a.exec();
}
the database is present in resources:
enter image description here
I want to see the whole path of a drive, I did it on Windows 8 and 9, the paths are displayed correctly.
But in Windows 2003 and 2008, the paths are not displayed, what's the problem?
My code
#include <QCoreApplication>
#include <QtCore>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDirIterator it("C:/",QStringList() << "*", QDir::Files , QDirIterator::Subdirectories);
while(it.hasNext())
{
qDebug() << it.next();
}
return a.exec();
}
picture app
I'm trying to setup translations for a qt app but it seems that the translations files are loaded after the window because the only translated text are the ones in messages box
This is the code:
#include <QApplication>
#include <QTranslator>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QCoreApplication::setOrganizationName("Lebbadi");
QCoreApplication::setOrganizationDomain("lebbadi.fr");
QCoreApplication::setApplicationName("zNavigo");
QTranslator translator;
if(translator.load(QLocale(QLocale::French), "app", "_", ":/translations"))
app.installTranslator(&translator);
else
exit(-1);
MainWindow window;
window.show();
return app.exec();
}
Thanks
Now, I have 1 application, but I don't want to open application twice, so I using QShareMemory to detect application when open twice.
And my question is: how I show current application in screen when user open application the second ?
int main(int argc, char *argv[]) {
Application a(argc, argv);
/*Make sure only one instance of application can run on host system at a time*/
QSharedMemory sharedMemory;
sharedMemory.setKey ("Application");
if (!sharedMemory.create(1))
{
qDebug() << "123123Exit already a process running";
return 0;
}
/**/
return a.exec();
}
Thanks.
Here's another approach in pure Qt way:
Use QLocalServer and QLocalSocket to check the existence of application and then use signal-slot mechanism to notify the existing one.
#include "widget.h"
#include <QApplication>
#include <QObject>
#include <QLocalSocket>
#include <QLocalServer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString appKey = "applicationKey";
QLocalSocket *socket = new QLocalSocket();
socket->connectToServer(appKey);
if (socket->isOpen()) {
socket->close();
socket->deleteLater();
return 0;
}
socket->deleteLater();
Widget w;
QLocalServer server;
QObject::connect(&server,
&QLocalServer::newConnection,
[&w] () {
/*Set the window on the top level.*/
w.setWindowFlags(w.windowFlags() |
Qt::WindowStaysOnTopHint);
w.showNormal();
w.setWindowFlags(w.windowFlags() &
~Qt::WindowStaysOnTopHint
);
w.showNormal();
w.activateWindow();
});
server.listen(appKey);
w.show();
return a.exec();
}
But if you're using Qt 5.3 on Windows, there's a bug for QWidget::setWindowFlags and Qt::WindowStaysOnTopHint, see https://bugreports.qt.io/browse/QTBUG-30359.
Just use QSingleApplication class instead of QApplication:
https://github.com/qtproject/qt-solutions/tree/master/qtsingleapplication
int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);
if (app.isRunning())
return 0;
MyMainWidget mmw;
app.setActivationWindow(&mmw);
mmw.show();
return app.exec();
}
It is part of Qt Solutions: https://github.com/qtproject/qt-solutions