Qt 101: Why can't I use this class? - c++

I have experience with C++ but I've never really used Qt before. I'm trying to connect to a SQLite database, so I found a tutorial here and am going with that. In the QtCreator IDE, I went to Add New --> C++ Class and in the header file pasted in the header the header from that link and in the .cpp file I pasted the source. My main.cpp looks like this:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "databasemanager.h"
#include <qlabel.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
DatabaseManager db();
QLabel hello("nothing...");
if(db.openDB()){ // Line 13
hello.setText("Win!");
}
else{
hello.setText("Lame!");
}
hello.resize(100, 30);
hello.show();
return a.exec();
}
And I'm getting this error:
main.cpp:13: error: request for member 'openDB' in 'db', which is of non-class type 'DatabaseManager()'
Can anyone point me in the right direction? I know "copypaste" code isn't good, I just wanted to see if I could get DB connectivity working and I figured something like this would be simple... thanks for the help.

Change the DatabaseManager line to:
DatabaseManager db;
You're declaring a local function called db that takes no parameters and returns a DatabaseManager object when you provide the ();

Related

Dark theme does not apply to the title bar

I have applied a dark style to my application, so good until then, my question is why the style is not applied to the title bar of my application, and the rest of the forms that open in me application, as you can see, it stays white and looks very bad, any suggestions would be appreciated.
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile f(":/qdarkstyle/style.qss");
f.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream ts(&f);
a.setStyleSheet(ts.readAll());
MainWindow w;
w.show();
return a.exec();
}
I got the subject from here.
https://github.com/ColinDuquesnoy/QDarkStyleSheet
[Works on Qt 6.2.2, not sure about Qt5]
There might be a configuration file qt.conf in the same folder as your executable file. If it does not exist, create. And add the lines
[Platforms]
WindowsArguments = darkmode=1
Use darkmode=2 if you also want to change the title bar colour on the fly.

C++ Qt How do you make a label display a movie (gif) that you created using the form?

I have created a label in the form UI and I would like to make it display a GIF using code in main. This is what I have so far but I'm not sure what to put in place of the "???????". How can I make this label display a GIF?
#include "dialog.h"
#include <QApplication>
#include <QMovie>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
QMovie *movie = new QMovie(":/Gifs/Welcome_animation2.gif");
QLabel *lblMovie = new QLabel();
lblMovie->setMovie(??????????);
movie->start();
w.show();
return a.exec();
}
As pointed by CoryKramer, you should just use lblMovie->setMovie(movie). However, the problem seens to be that you're not setting the movie to the correct QLabel (ie. lblMovie is not the same object shown on your window)
You probably have to create the QMovie inside your Dialog class, and there you set the movie to the QLabel by using ui->lblMovie->setMovie(movie). If it really must be created outside, then you should add a public method to allow it.
Take a look at http://doc.qt.io/qt-5/gettingstartedqt.html for more information about proper usage of ui files

Creating a QVideoWidget in Qt5

I have the following piece of code:
#include <QtWidgets/QtWidgets>
#include <QtMultimedia/QCamera>
#include <QtMultimedia/QMediaPlayer>
int main(int argc, char * argv[])
{
QApplication testQt(argc, argv);
QMainWindow w;
QWidget videoContainer(&w);
w.setCentralWidget(&videoContainer);
QVideoWidget videoWidget(&videoContainer);
QCamera cam(&w);
cam.setViewfinder(&videoWidget);
cam.start();
w.show();
return testQt.exec();
}
in which I am trying to create a main window, create a container widget to display video, create a videowidget in that container, and then finally set the viewfinder of the camera to that videowidget. However, when I try to do this I get the error
Variable has incomplete type 'QVideoWidget'
Why am I getting this error?
You need to include the corresponding header as follows:
#include <QVideoWidget>
You may also need to add this to your project file:
QT += multimediawidgets

Qt Creator _IMP error while connecting to MySql

I'm trying to create an app that connects to my local MySql through XAMPP and outputs the data into a tableWidget.
After writing all the code for it and correcting all the mistakes, I'm getting 17 errors about "undefined reference to `_imp__ZN.....' "
1st Question: Do I have to put all my project files inside XAMPP/htdocs like I did using PHP?
2nd Question: I've only written something inside main.cpp, did I miss something for other classes or maybe I need something else inside the header file?
I've heard that "imp_" problems are related to the compiler, but I'm not sure what to do!
Without further ado, this 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>
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());
}
MainWindow w;
w.show();
return a.exec();
}
And here are my errors:
http://i57.tinypic.com/nodow7.png
Thank you for your help!
Well, the errors mean that you are missing a library containing the necessary symbols. If you are using qmake to build the project you have to enable SQL via "QT += sql". Otherwise you will have to find and add the library manually.

mcvs qwt - must construct a QApplication before a QPaintDevice

I would like to ask if you could help me with my app.
I have an application in C++ using Qt (and Qwt) in MCVS 2010. I want to open QDialog window with QwtPlot on button click from Main Window (QMainWindow). Here is some code:
MainWindow.cpp
void MainWindow::on_pushButton_1_clicked ()
{
Dialog_plot dp;
dp.setModal(true);
dp.exec();
}
Dialog_plot.cpp:
#include <qwt_plot.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include "Dialog_plot.h"
Dialog_plot::Dialog_plot(QWidget *parent)
{
plot = new QwtPlot();
//more code...
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This code is compiling, but when I click on that pushbutton_1 in my application I'm getting error:
Must construct a QApplication before a QPaintDevice
I know that such error was discussed many times and I was reading a lot about it but I can't see solution for my problem.
One more thing I would like to mention - I have similar application with Qwt plot written by somebody else and his application compiles and works without any problems in my MCVS. I was trying to compare Linker/libraries included but it seems to be the same. So I guess there is a problem with my application, I just can't solve it. I really need some help!