QT declare Mainwindow out of the main class - c++

I have this two class in c++
GUI.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() {
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w.show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
// TODO Auto-generated destructor stub
}
GUI.h
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mainwindow.h"
#ifndef GUI_H_
#define GUI_H_
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow w;
};
#endif
But when i run this program i have the error:
QWidget: Must construct a QApplication before a QPaintDevice
How can I declare MainWindow w in gui.h in such a way that I don't receive this error

You can't (well, you can, but you shouldn't). The MainWindon declaration is right where it should be. The problem is that you attempt to create a GUI object before you create the QApplication.
Why not create the QApplication where you create the GUI object, just before it?

I would have made w a pointer used a forward declaration for MainWindow and removed all the includes (including the 2 includes for mainwindow.h) from GUI.h. Then like the answer from Sebastian says construct the QApplication first.
AL_GUI.h
#ifndef GUI_H_
#define GUI_H_
class MainWindow;
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow* w;
};
gui.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() : w(NULL)
{
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w = new MainWindow;
w->show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
delete w;
}

Related

Access widgets in QMainWindow from another class

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!");
}

Share data between two QWidget instances

I would like share a string between two instances of QWidget.
In main.cpp, two objects are instantiated and shown like this:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w1,w2; //Derived from QWidget
w1.show();
w2.show();
return a.exec();
}
I would introduce SharedState class:
// shared_state.h
#ifndef SHARED_STATE_HPP
#define SHARED_STATE_HPP
#include <QObject>
class SharedState : public QObject
{
Q_OBJECT
public:
SharedState(QString initialValue = "")
: currentValue(initialValue)
{}
QString getCurrentValue()
{
return currentValue;
}
public slots:
void setValue(QString newValue)
{
if(currentValue != newValue)
{
currentValue = newValue;
emit valueChanged(currentValue);
}
}
signals:
void valueChanged(QString);
private:
QString currentValue;
};
#endif // SHARED_STATE_HPP
Now I would provide reference to SharedState in Dialog's constructor,
// dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QWidget>
#include "shared_state.h"
namespace Ui {
class Dialog;
}
class Dialog : public QWidget
{
Q_OBJECT
public:
explicit Dialog(SharedState& state, QWidget *parent = 0);
~Dialog();
private slots:
void handleTextEdited(const QString&);
public slots:
void handleInternalStateChanged(QString);
private:
Ui::Dialog *ui;
SharedState& state;
};
#endif // DIALOG_H
You may have noticed that I have added two slots, one to handle the case when the text is manually edited and one when shared state will inform us that we are out of date.
Now in Dialog's constructor I had to set initial value to textEdit, and connect signals to slots.
// dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(SharedState& state, QWidget *parent) :
QWidget(parent),
ui(new Ui::Dialog),
state(state)
{
ui->setupUi(this);
ui->textEdit->setText(state.getCurrentValue());
QObject::connect(ui->textEdit, SIGNAL(textEdited(QString)),
this, SLOT(handleTextEdited(QString)));
QObject::connect(&state, SIGNAL(valueChanged(QString)),
this, SLOT(handleInternalStateChanged(QString)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::handleTextEdited(const QString& newText)
{
state.setValue(newText);
}
void Dialog::handleInternalStateChanged(QString newState)
{
ui->textEdit->setText(newState);
}
Now the change in the main function:
// main.cpp
#include "dialog.h"
#include "shared_state.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SharedState state("Initial Value");
Dialog w1(state), w2(state);
w1.show();
w2.show();
return a.exec();
}

C++ accessing object, QT

I'd appreciate it, when someone could help me. I am not used to C++. I am new to it.
My problem: I want to make an object "player" usable for another class. I don't get how to archive this.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#include <QMainWindow>
#include <QMediaPlayer>
#include <QDebug>
#include "Leap.h"
#include <leapmotionlistener.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QMediaPlayer* player;
private slots:
....
private:
Ui::MainWindow *ui;
void initialize();
Controller controller;
LeapMotionListener leapMotionListener;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSound>
#include <iostream>
#include <QfileDialog>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::on_positionChanged);
connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationChanged);
initialize();
}
QString path;
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initialize(){
controller.addListener(leapMotionListener);
leapMotionListener.setPlayer(player);
controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
}
...
leapmotionlistener.h
#ifndef LEAPMOTIONLISTENER
#define LEAPMOTIONLISTENER
#include <iostream>
#include <cstring>
#include "Leap.h"
#include <QFile>
#include <QAudioOutput>
#include <QMediaPlayer>
using namespace Leap;
class LeapMotionListener : public Listener {
public:
LeapMotionListener();
void setPlayer(QMediaPlayer&);
//... some more methods
private:
QMediaPlayer player;
};
#endif // LEAPMOTIONLISTENER
leapmotionlistener.cpp
//calls the player object in one method like the following
player.stop();
My main questions are: What am I doing wrong while referencing and instantiating? And how can leapmotionlistener access the same player object (I want to influence the audio)?
I get the Error:
MusicPlayer\mainwindow.cpp:32: Error: C2664: 'void LeapMotionListener::setPlayer(QMediaPlayer &)' : converting from argument 1 'QMediaPlayer *' in 'QMediaPlayer &' not possible
This little project is downloadable for a quick view with the following link:
https://www.dropbox.com/s/igr7ywnvicdlxxd/MusicPlayer.zip?dl=0
Thanks in advance!
All you need to do is
leapMotionListener.setPlayer(*player);
An lvalue reference binds to an object, not to the pointer to the object. In order to get the object from a pointer to it, you need to dereference the pointer using the *.
Among other problems, the QMediaPlayer you're trying to pass to setPlayer is a pointer, not a reference to an object.
You can either instantiate your player object like this:
QMediaPlayer player;
Or you can change your setPlayer function signature to this:
void setPlayer(QMediaPlayer* mplayer);
If you update your function signature, you'll also need fix other function signatures and leave the ampersands out of your connect() statements when you're referencing the player.
the following worked now:
in MainWindow.h:
public:
QMediaPlayer* player;
in MainWindow.cpp:
player = new QMediaPlayer(this);
leapMotionListener.setPlayer(player);
controller.addListener(leapMotionListener);
in LeapMotionListener.h:
private:
QMediaPlayer *player;
public:
void setPlayer(QMediaPlayer *mplayer);
in LeapMotionListener.cpp:
LeapMotionListener::LeapMotionListener()
: player(0)
{}
void LeapMotionListener::setPlayer(QMediaPlayer *mplayer){
player = mplayer;
}

Appcrash when i try to add QTabWidget with a button

i just started learning Qt few days ago, and i have a problem that i can't solve.
First there is the files :
main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
test w;
w.show();
return a.exec();
}
test.h
#ifndef TEST_H
#define TEST_H
#include <QWidget>
#include <QTabWidget>
#include <QTextEdit>
#include <QPushButton>
class test : public QWidget
{
Q_OBJECT
public:
test();
~test();
private slots:
void addT();
private :
QTabWidget *tab;
QPushButton *b,*c;
};
#endif // TEST_H
and test.cpp
#include "test.h"
test::test()
{
QTabWidget *tab = new QTabWidget(this);
QPushButton *b = new QPushButton("Add",this);
tab->addTab(b,"test");
QObject::connect(b,SIGNAL(clicked()),this,SLOT(addT()));
}
test::~test()
{
}
void test::addT()
{
QPushButton *c= new QPushButton("Add",this);
tab->addTab(c,"test");
}
the program starts normally but when i push the button to add a new Tab it crashes
Please Help me!
In your constructor you are not assigning to the QTabWidget and QPushButton instanced declared in your header, but are creating two new instances (with the same name) that will be gone at the end of the scope. The tab instance is still a nullptr and when trying to derefence it in addT, your program will crash. You need to assign to the variables declared in test.h like this:
test::test() : tab(new QTabWidget(this), b(new QPushButton("Add", this) {
...
}

QT and C++: can't call function outside main() function

I would like to know how to call the function Do_Download() from the SocketTest class outisde the main() function.
The first cTest.Do_Download() does work, but when I call the test() function, the csTest.Do_Download() does not work.
So it looks like I can only acces SocketTest from inside the main() function, and not from any other function.
Does somebody know how this can be solved?
Thanks!
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
SocketTest cTest;
cTest.Do_Download();
return a.exec();
}
void test()
{
qDebug() << "test main functie";
SocketTest csTest;
csTest.Do_Download();
}
SocketTest.h:
#ifndef SOCKETTEST_H
#define SOCKETTEST_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QHttp>
#include <QFile>
#include <QString>
class SocketTest : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = 0);
void Do_Download();
signals:
public slots:
void stateChanged ( int state );
void responseHeaderReceived ( const QHttpResponseHeader & resp );
void requestFinished ( int id, bool error );
private:
QTcpSocket *socket;
QHttp *http;
QHttp *http2;
};
#endif // SOCKETTEST_H
If your DoDownload function is doing anything asynchronously (likely, when dealing with the Qt networking classes), the SocketTest you are creating in test() is being destroyed before it can act on any return value.
It works in main() because the event loop starts and the SocketTest instance hangs around.