Just trying to make a simple web viewer app so I can view a webpage in a widget and I am getting the error:
QWidget: Must construct a QApplication before a QPaintDevice
I feel like this is a problem with the webkit? I am statically linking this project.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QWebPage>
#include <QtWebKit>
#include "Windows.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebView *view = new QWebView(parent);
view->load(QUrl("http://google.com/"));
view->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
.pro file
QT += core gui webkit network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = webkittest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
header file
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebView>
#include <QUrl>
#include <QtPlugin>
#include <QGridLayout>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QMessageBox>
#include <QDialog>
#include <QtWebKit>
class QAction;
class QCheckBox;
class QComboBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QMenu;
class QPushButton;
class QSpinBox;
class QTextEdit;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
//void closeEvent(QCloseEvent *event);
//void keyPressEvent(QKeyEvent *);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
private slots:
//void onSslErrors(QNetworkReply* reply, const QList<QSslError> &errors);
private:
Ui::MainWindow *ui;
QWebView* m_pWebView;
#endif // MAINWINDOW_H
Any ideas what could be causing that?
Fixed this by installing the latest version of Qt framework and dynamically linking webkit as they have disable static linking.
Related
Although it is a frequently asked question, and I've tried many ways including those from SO, like Trying to access widgets of MainWindow from another class, However I still cannot work out a solution, below is my code which reported error "Unknown type name 'CustomClass'" in mainwindow.h:
Thanks in advance for any help!
customclass.h
#ifndef CUSTOMCLASS_H
#define CUSTOMCLASS_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
class MainWindow;
class CustomClass
{
public:
CustomClass(MainWindow *parent);
MainWindow * mainWindow;
void testFunc();
};
#endif // CUSTOMCLASS_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "customclass.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
Ui::MainWindow *ui;
CustomClass *customClass = new CustomClass(this);
};
#endif // MAINWINDOW_H
customclass.cpp
#include "customclass.h"
CustomClass::CustomClass(MainWindow *parent)
{
this->mainWindow = parent;
}
void CustomClass::testFunc()
{
mainWindow->ui->label->setText("Hello World!");
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
customClass->testFunc();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
please read about Circular Dependencies in C++ .
you have problems because you create a loop, you included #include "customclass.h" in MainWindow class and also #include "mainwindow.h" in CustomClass .
it's better that you don't use MainWindow in other classes and add CustomClass objects in MainWindow. The idea is that MainWindow is your root window class and we create one object from it in main.cpp you can do what you ask but logically it's not good.
as we can see in QMainWindow Class document.
The QMainWindow class provides a main application window
A main window provides a framework for building an application's user
interface.
This is your base, you should add the feature you want inside this class not add it in other widgets.
It's the main class for other widgets. This is a clean way to code and if you look at big projects in GitHub you will see this.
move #include "mainwindow.h" and #include "ui_mainwindow.h" inside customclass.h into customclass.cpp before #include "customclass.h" and it works! Thank you!#drescherjm
The fixed code is shown below:
customclass.h
#ifndef CUSTOMCLASS_H
#define CUSTOMCLASS_H
class MainWindow;
class CustomClass
{
public:
CustomClass(MainWindow *parent);
~CustomClass();
MainWindow *mainWindow;
void testFunc();
};
#endif // CUSTOMCLASS_H
customclass.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"//Move these two lines from the header to here
#include "customclass.h"
CustomClass::CustomClass(MainWindow *parent)
{
this->mainWindow = parent;
}
CustomClass::~CustomClass()
{
}
void CustomClass::testFunc()
{
mainWindow->ui->label->setText("Hello World!");
}
I'm trying to add an image to a QLabel on a Qt creator program. However when compiling the program I get an error: C:\Users\*****\Documents\GameAPP\main.cpp:12: error: C2027: use of undefined type 'QBitmap'
Using Qt version 5.7 and 5.6 on Windows 10.
This is my code:
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QLabel *l = new QLabel();
QPixmap p("C:\Users\*****\Pictures\Start_Orb.png");
l->setPixmap(p);
l->setMask(p.mask()); //error at this line
l->setFixedSize(20, 20);
l->move(20, 20);
l->show();
return a.exec();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
You only need to add #include <QBitmap> and it will work.
I've created my own class in QT 5.4 with header and source file, then I've implemented there a video player. The problem is, that it's working fine when I'm placing it in the center of the main window or when I'm showing it in a separate window, but when I'm trying to create a Widget container in the main window and promote widget to it, it gives me some errors:
'videowindow' does not name a type
'widget' was not declared in this scope
expected type-specifier before 'videowindow'
So, here is the code:
videowindow.h - header of my implemented class
#ifndef VIDEOWINDOW_H
#define VIDEOWINDOW_H
#include "mainwindow.h"
class videoWindow
{
private:
QMediaPlayer *player;
QMediaPlaylist *playlist;
public:
videoWindow();
void video_enable();
void widget();
};
#endif // VIDEOWINDOW_H
videowindow.cpp - source
#include "videowindow.h"
#include "mainwindow.h"
videoWindow::videoWindow()
{
}
void videoWindow::video_enable()
{
player = new QMediaPlayer;
playlist = new QMediaPlaylist;
playlist->addMedia(QUrl::fromLocalFile("D:\\фото\\video\\P1150277.MOV"));
QVideoWidget *videoWid = new QVideoWidget;
player->setVideoOutput(videoWid);
videoWid->show();
player->setPlaylist(playlist);
playlist->setCurrentIndex(1); // start from a first video in the playlist
player->play();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtMultimedia>
#include <QtMultimediaWidgets>
#include <QtDebug>
#include <string>
#include <QGraphicsVideoItem>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "videowindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include "videowindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
InstantPlayer.pro - .pro file of the project
#-------------------------------------------------
#
# Project created by QtCreator 2016-06-24T11:37:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = InstantPlayers
TEMPLATE = app
#
QT += multimedia
QT += multimediawidgets
#
SOURCES += main.cpp\
mainwindow.cpp \
videowindow.cpp \
playerbase.cpp
HEADERS += mainwindow.h \
videowindow.h \
playerbase.h
FORMS += mainwindow.ui \
playerbase.ui
I have 3 classes.
class with a mainwindow which comes from the designer(ui-file)
class wich will manage database stuff like inserts
controller class. I want to extend the whole thing to networkcommunication later.
My problem:
I want to connect a simple QPushButton ui->addbutton from the window class with a slot addEntry from the databaseclass, but I get this error :
ERROR: no matching function for call to
'generalControler::connect(QPushButton*, const char*, dbmanager*&,
const char*)'
mydb, SLOT(addEntry()));
//no difference with &mydb or *mydb
MainWindow(0x13f57988, name = "MainWindow") QPushButton(0x13f5f3e0, name = "addButton")
MainWindow(0x13f57988, name = "MainWindow") 0x13f5f3e0//<--?????
//<=Here u see the adresses printed with Qdebug(). top: mainwindowclass. bot: generalcontrolerclass
//why there is missing information after returning the adress of a 'ui->addButton' to another class? Is this maybe the problem?
main.cpp
#include <QApplication>
#include "generalcontroler.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
generalControler controler;
return a.exec();
}
generalcontroler.h
#ifndef GENERALCONTROLER_H
#define GENERALCONTROLER_H
#include <QApplication>
#include "mainwindow.h"
#include "dbmanager.h"
class generalControler : public QObject
{
Q_OBJECT
public:
generalControler();
};
#endif // GENERALCONTROLER_H
generalcontroler.cpp
#include "generalcontroler.h"
#include <QDebug>
generalControler::generalControler(){
MainWindow* window = new MainWindow;
window->show();
dbmanager* mydb= new dbmanager("path_to_my_database.db", window);
mydb->addEntry();
qDebug()<<window->getThis()<<window->getcloseButton();
connect(window->getaddButton(), SIGNAL(clicked()),
mydb, SLOT(addEntry()));
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton* getaddButton();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
}
QPushButton* MainWindow::getaddButton()
{
return ui->addButton;
}
dbmanager.h
#ifndef DBMANAGER_H
#define DBMANAGER_H
#include <QSqlDatabase>
#include <QDebug>
#include "mainwindow.h"
class dbmanager: public QObject{
Q_OBJECT
public:
dbmanager(const QString& path);
public slots:
void addEntry();
private:
QSqlDatabase mydatabase;
};
#endif // DBMANAGER_H
dbmanager.cpp
#include "dbmanager.h"
dbmanager::dbmanager(const QString& path)
{
mydatabase = QSqlDatabase::addDatabase("QSQLITE");
mydatabase.setDatabaseName(path);
if (!mydatabase.open())
{
qDebug() << "Error: connection fail";
}
else
{
qDebug() << "Database: connection ok";
}
}
void dbmanager::addEntry()
{
qDebug()<<"addEntry success";
}
I was searching for 6 hours but I never saw such an example with 2 classes, a controler and an ui-file. Could anyone help me?
The connect looks good to me. Try if #include <QPushButton> in generalcontroler.cpp helps. If the compiler knows about QPushButton only by forward-declaration, it doesn't know that it's a QObject and thus the connect() signatures (with QObject* in it) don't match.
This question should hopefully be easy to answer. I created a few buttons in my MainWindow using Qt Creator, and when I go to write the functions for the buttons, the compiler says they were not declared in this scope. What do I need to #include for these objects to be declared? The compiler error for the following would be 'baseDir' was not declared in this scope. baseDir is the objectName for a lineEdit in my window.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void getDir();
void createProj();
private slots:
void on_findDir_clicked();
void on_create_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include <QtGui/QApplication>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_findDir_clicked()
{
QString path;
path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null );
baseDir->setText( path );
}
The items you define in your .ui file are not added directly to your main window class, they are added to its ui membre.
Try with:
ui->baseDir->setText( path );
Look at the ui_mainwindow.h file that is generated during the build if you're curious.