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
Related
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
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();
}
The exact crash when debugging is:
The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x7fed96c6cda, code: 0x0000005: read access violation at: 0x0, flags=0x0.
The exception then points to this line in the below code:
if(QOpenGLContext::currentContext()->isValid())
The below code is enough to reproduce the exception for me.
Subclassing the QOpenGLWidget class and making the subclass call initializeGL() once before attempting to access context does not fix problem.
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLContext>
#include <QDebug>
void initialize(QOpenGLWidget * renderArea)
{
renderArea->makeCurrent();
if(QOpenGLContext::currentContext()->isValid())
{
qInfo() << "Valid.";
}
}
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setVersion(3,3);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
QApplication a(argc, argv);
QOpenGLWidget * glw = new QOpenGLWidget;
initialize(glw);
return a.exec();
}
I now realize the answer: You have to do all such opengl initialization AFTER the event loop starts.
A fixed code where the "MainWindow" class does all opengl initialization on receiving signal "onEventLoopStarted":
#include "mainwindow.h"
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLContext>
#include <QDebug>
#include <QTimer>
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setVersion(3,3);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
QApplication a(argc, argv);
MainWindow w;
w.resize(512, 512);
w.show();
QTimer::singleShot(0, &w, SLOT(onEventLoopStarted()));
return a.exec();
//Window receives event and begins to initialize.
}
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
I'm having a little problem once again and maybe you guys could help!
So I'm trying to display database query inside a tableWidget.
Problem is that instead of showing up inside the tableWidget that I've created in the GUI, it opens a completely new program with just that query(without my other buttons or labels).
Screenshot of UI: http://i57.tinypic.com/206eety.png
Instead it looks like this: http://i60.tinypic.com/jkvjuh.png
Question: How do I get that table inside my tableWidget?
Here is my main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QtSql/QSql>
#include <QtGui/QtGui>
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QTableWidget>
#include <QtSql/QSqlError>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlQueryModel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget* tableWidget = new QTableWidget();
tableWidget->setWindowTitle("Connect to Mysql Database");
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("pizzeria mabo");
db.setUserName("root");
db.setPassword("");
if (!db.open())
{
QSqlError err = db.lastError();
QMessageBox::critical(0, QObject::tr("Database Error"), err.text());
}
QSqlQuery query("SELECT * FROM kunden");
tableWidget->setColumnCount(query.record().count());
tableWidget->setRowCount(query.size());
int index=0;
while (query.next())
{
tableWidget->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
tableWidget->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));
tableWidget->setItem(index,2,new QTableWidgetItem(query.value(2).toString()));
tableWidget->setItem(index,3,new QTableWidgetItem(query.value(3).toString()));
tableWidget->setItem(index,4,new QTableWidgetItem(query.value(4).toString()));
tableWidget->setItem(index,5,new QTableWidgetItem(query.value(5).toString()));
tableWidget->setItem(index,6,new QTableWidgetItem(query.value(6).toString()));
tableWidget->setItem(index,7,new QTableWidgetItem(query.value(7).toString()));
tableWidget->setItem(index,8,new QTableWidgetItem(query.value(8).toString()));
index++;
}
tableWidget->show();
return a.exec();
MainWindow w;
w.show();
return a.exec();
}
Thank you for the help!