qt compiler error on instantiating class - qt

I am having a problem compiling dialog.h, the compiler complains that QHostAddress::Any is not a type, and expected identifier before numeric constant. (both on the second to last line of dialog.h).
Can someone tell me why this won't compile? I'm instantiating the server object, and passing parameters that the server constructor expects...i thought...
dialog.h
#include <QWidget>
#include <QHostAddress>
#include "server.h"
class QLabel;
class QPushButton;
class Dialog : public QWidget
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
private:
QLabel *statusLabel;
QPushButton *quitButton;
Server server;
};
server.h:
class Server : public QTcpServer
{
Q_OBJECT
public:
Server(QHostAddress listenAddress, quint16 listenPort, QObject *parent = 0);
QHostAddress hostAddress;
quint16 hostPort;
protected:
void incomingConnection(qintptr socketDescriptor);
private:
};
dialog.cpp (partial)
Dialog::Dialog(QWidget *parent)
: QWidget(parent), server(QHostAddress::Any, 4000)
{
server.cpp (partial)
#include "server.h"
#include "clientthread.h"
#include <stdlib.h>
Server(QHostAddress listenAddress, quint16 listenPort, QObject *parent = 0)
: hostAddress(listenAddress), hostPort(listenPort), QTcpServer(parent)
{
}
Note code above updated. Now compiler complains:
Expected ')' before 'listenAddress' on the constructor definition of Server.

You need to declare the Server object as a Dialog class member variable and than define it in constructor. Here is how the Dialog class should look like:
dialog.h
#include <QWidget>
#include <QHostAddress>
#include "server.h"
class QLabel;
class QPushButton;
class Dialog : public QWidget
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
private:
QLabel *statusLabel;
QPushButton *quitButton;
Server server; // Declare server member variable.
};
dialog.cpp
Dialog::Dialog(QWidget *parent)
:
QWidget(parent),
server(QHostAddress::Any, 4000) // construct server
{
//...
}

Related

Couldn't render OpenGLES context in Qt

I am trying to use OpenGLES in a desktop platform. I have a class whose name is GraphicsViewer which is derived from QOpenGLWidget. When I set the QSurfaceFormat::renderableType to QSurfaceFormat::OpenGLES, I get the following errors and nothing is drawn on the widget. Can you help me to create an OpenGLES context in a desktop platform? My OS is Windows 10 and my Qt version is 5.12.3.
Errors:
QOpenGLWidget: Failed to create context
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: QOpenGLContext creation failed
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
Here is my codes...
main.cpp
#include "QtOpenGLES.h"
#include <QApplication>
#include <QSurfaceFormat>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat f;
f.setVersion(2, 0);
f.setRenderableType(QSurfaceFormat::OpenGLES);
QSurfaceFormat::setDefaultFormat(f);
QtOpenGLES w;
w.show();
return a.exec();
}
QtOpenGLES.h
#ifndef QTOPENGLES_H
#define QTOPENGLES_H
#include <QMainWindow>
namespace Ui {
class QtOpenGLES;
}
class QtOpenGLES : public QMainWindow
{
Q_OBJECT
public:
explicit QtOpenGLES(QWidget *parent = nullptr);
~QtOpenGLES();
private:
Ui::QtOpenGLES *ui;
};
#endif // QTOPENGLES_H
QtOpenGLES.cpp
#include "QtOpenGLES.h"
#include "ui_QtOpenGLES.h"
QtOpenGLES::QtOpenGLES(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::QtOpenGLES)
{
ui->setupUi(this);
}
QtOpenGLES::~QtOpenGLES()
{
delete ui;
}
GraphicsViewer.h
#ifndef GRAPHICSVIEWER_H
#define GRAPHICSVIEWER_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
class GraphicsViewer : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
GraphicsViewer(QWidget* parent = nullptr);
// QOpenGLWidget interface
protected:
void initializeGL();
void paintGL();
};
GraphicsViewer.cpp
#include "GraphicsViewer.h"
#include <QtOpenGL>
GraphicsViewer::GraphicsViewer(QWidget *parent) :
QOpenGLWidget (parent)
{
}
void GraphicsViewer::initializeGL()
{
initializeOpenGLFunctions();
}
void GraphicsViewer::paintGL()
{
glClearColor(1, 0, 0, 1);
}

Connecting Signals and Slots between Child and Parent (invalid use of incomplete type)

I'm trying to connect signals from a child window (QDialog named VolumePage) to its parent (QMainWindow named MockUI). I'm running into a:
invalid use of incomplete type 'class Ui::VolumePage'
error when I attempt to make the connection.
I'm trying to connect inside the MockUI.cpp when I make the volume page. (Happens on a button press)
void MockUI::on_pushButton_3_clicked()
{
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
I've made Ui::VolumePage *ui; public.
Here's the entire error:
error: invalid use of incomplete type 'class Ui::VolumePage'
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
Can anyone help me understand what the problem is, or another clean way to do what I'm trying to do?
Edit: (Additional Source)
VolumePage.h:
#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H
#include <QDialog>
namespace Ui {
class VolumePage;
}
class VolumePage : public QDialog
{
Q_OBJECT
public:
explicit VolumePage(QWidget *parent = 0);
Ui::VolumePage *ui;
~VolumePage();
private slots:
void on_verticalSlider_valueChanged(int value);
private:
};
#endif // VOLUMEPAGE_H
mockUI.cpp:
#include "mockUI.h"
#include "ui_mock_ics.h"
#include <QLCDNumber>
#include "volumepage.h"
MockUI::MockUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MockUI)
{
ui->setupUi(this);
}
MockUI::~MockUI()
{
delete ui;
}
void MockUI::on_pushButton_3_clicked()
{
//Modal Approach
//VolumePage volPage;
//volPage.setModal(true);
//volPage.exec();
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
volumepage.cpp:
#include "volumepage.h"
#include "mockUI.h"
#include "ui_volumepage.h"
VolumePage::VolumePage(QWidget *parent) :
QDialog(parent),
ui(new Ui::VolumePage)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
//Mock_ICS* ics = (Mock_ICS*)parent;
//connect(ui->verticalSlider,SIGNAL(valueChanged(int)),ics->ui->progressBar,SLOT(setValue(int)));
//connect(ui->verticalSlider,SIGNAL(valueChanged(int)),this->parent()->progressBar,SLOT(setValue(int)));
}
VolumePage::~VolumePage()
{
delete ui;
}
Ui::VolumePage *ui should remain private. Instead create a signal on the volPage, something like verticalSliderValueChanged(int value). See below...
mockui.h
#ifndef MOCKUI_H
#define MOCKUI_H
#include <QMainWindow>
#include "volumepage.h"
namespace Ui {
class MockUI;
}
class MockUI : public QMainWindow
{
Q_OBJECT
public:
explicit MockUI(QWidget *parent = 0);
~MockUI();
VolumePage* volPage;
private slots:
void on_pushButton_3_clicked();
private:
Ui::MockUI *ui;
};
#endif // MOCKUI_H
mockui.cpp
#include "mockui.h"
#include "ui_mockui.h"
#include "volumepage.h"
MockUI::MockUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MockUI)
{
ui->setupUi(this);
}
MockUI::~MockUI()
{
delete ui;
}
void MockUI::on_pushButton_3_clicked()
{
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage,SIGNAL(verticalSliderValueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
volumepage.h
#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H
#include <QDialog>
namespace Ui {
class VolumePage;
}
class VolumePage : public QDialog
{
Q_OBJECT
public:
explicit VolumePage(QWidget *parent = 0);
~VolumePage();
signals:
void verticalSliderValueChanged(int value);
private slots:
void on_verticalSlider_valueChanged(int value);
private:
Ui::VolumePage *ui;
};
#endif // VOLUMEPAGE_H
volumepage.cpp
#include "volumepage.h"
#include "ui_volumepage.h"
VolumePage::VolumePage(QWidget *parent) :
QDialog(parent),
ui(new Ui::VolumePage)
{
ui->setupUi(this);
}
VolumePage::~VolumePage()
{
delete ui;
}
void VolumePage::on_verticalSlider_valueChanged(int value)
{
emit verticalSliderValueChanged(value);
}
This is an example of the pImpl idiom in C++. Class VolumePage is the one you should be using - this is the "public" interface of what Qt Creator has generated. Ui::VolumePage contains the complicated details you should not worry about.
So, in your code you should be using VolumePage, not Ui::VolumePage. You shouldn't have make Ui::VolumePage public.

Connect button to a function QT c++

I'm trying to connect a button to a function and after countless amounts of searches I couldn't find anything.
I could use a on_button_clicked(goto slot) function but I plan on re-assigning the button to run more code, so I decided to use the connect() class but when I run the application nothing happens when I type into the text box and click the game_user_input_submit button I have created, and im not sure if it is even calling the function.
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QtCore>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
user_name_set = false;
ui->game_chat_box->append("hi"); // a text edit box i created
if(user_name_set == false)
{
connect(ui->game_user_input_submit, SIGNAL(clicked()), SLOT(setUsername()));
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setUsername()
{
username = ui->game_user_input->text(); // get text from line edit i created
user_name_set = true;
ui->game_chat_box->append("BOT: Welcome " + username);
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void setUsername();
~MainWindow();
private slots:
//void on_game_user_input_submit_clicked();
private:
bool user_name_set = false;
QString username;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
oh and I'm kinda new to programming c++ in general so if anybody does give me some usefull information I might need it to explain it to me, sorry
connect should be of the form:
connect(object_pointer, signal_name, slot_name)
In your case, you are setting the slot name to setUsername() which is not a slot - it is just a public function.
Your mainwindow.h should be:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void setUsername();
private:
bool user_name_set = false;
QString username;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

Create an instance of QScopedPointer later

For example here is my code
QScopedPointer<QTimer> timer2(new QTimer);
But I want to define
QScopedPointer<QTimer> timer2;
in mainwindow.h and create an instance
timer2(new QTimer);
in the mainwindow.cpp
How?
Try the following:
// mainwindow.h
class MainWindow : public QMainWindow
{
private:
QScopedPointer<QTimer> timer2;
};
If you want to create the instance in the constructor, use the following:
// mainwindow.cpp
MainWindow::MainWindow()
:timer2(new QTimer)
{
}
Alternately, if you want to create the instance in some arbitrary member function of MainWindow, use this:
// mainwindow.cpp
void MainWindow::someFunction()
{
timer2.reset(new QTimer);
}
It's also worth reviewing initialization lists in C++ and the documentation for QScopedPointer.
Use method reset of QScopedPointer
timer2.reset(new QTimer());
What you're doing amounts to a premature pessimization. You're creating members of a MainWindow class separately and individually on the heap, when you should be simply putting them into the class as members:
// interface
#include <QMainWindow>
#include <QTimer>
class MainWindow : public QMainWindow {
Q_OBJECT
QTimer m_timer;
public:
MainWindow(QWidget * parent = 0, Qt::WindowFlags flags = 0);
};
// implementation
MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags) :
QMainWindow(parent, flags),
m_timer()
{
...
}
Of course, you would ordinarily not want to expose all the details of the MainWindow's implementation in the interface (header) file. Thus you would leverage the PIMPL idiom:
// interface
#include <QMainWindow>
class MainWindowPrivate;
class MainWindow : public QMainWindow {
Q_OBJECT
Q_DECLARE_PRIVATE(MainWindow)
QScopedPointer<MainWindowPrivate> const d_ptr;
public:
MainWindow(QWidget * parent = 0, Qt::WindowFlags flags = 0);
}
// implementation
#include "MainWindow.h"
#include <QTimer>
class MainWindowPrivate {
public:
QTimer timer;
}
MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags) :
QMainWindow(parent, flags),
d_ptr(new(MainWindowPrivate())
{
Q_D(MainWindow);
d->timer.start( ... );
...
}

Qt: QObject::connect: Cannot connect (null)

I'm trying to connect a signal from a QProcess inside my mainwindow() object to another QObject based class inside my mainwindow() object but I get this error:
QObject::connect: Cannot connect (null)::readyReadStandardOutput () to (null)::logReady()
Heres the code, its not complete by any means but I don't know why it doesn't work.
exeProcess.h
#ifndef EXEPROCESS_H
#define EXEPROCESS_H
#include <QObject>
class exeProcess : public QObject
{
Q_OBJECT
public:
explicit exeProcess(QObject *parent = 0);
signals:
void outLog(QString outLogVar); //will eventually connect to QTextEdit
public slots:
void logReady();
};
#endif // EXEPROCESS_H
exeProcess.cpp
#include "exeprocess.h"
exeProcess::exeProcess(QObject *parent) :
QObject(parent)
{
}
void exeProcess::logReady(){
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include "exeprocess.h"
/*main window ---------------------------------------*/
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QProcess *proc;
exeProcess *procLog;
public slots:
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);
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));
}
MainWindow::~MainWindow()
{
delete ui;
}
Thanks!.
You need to create the proc and procLog objects.
You've only got pointers as class members, so you'll have to initialize those (with new). connect only works on live objects.
proc is a pointer, but it does not point to anything. You have to instantiate a qprocess before you connect it!
proc = new QProcess();
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));

Resources