Qt QOpenGLFunctions not declared in this scope - qt

I'm creating a sort of drawable object class for mesh data and i'm getting this linker error. This is also on top of another class that handles the drawing of the meshes that was used in one of the Qt tutorials. I'm also using a QGLWidget to be the surface i'm drawing to. Here's what the header file looks for the first class.
#ifndef GLOBJECT_H
#define GLOBJECT_H
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QVector3D>
#include <QVector2D>
#include <QDebug>
#include <QFile>
struct VertexData
{
QVector3D position;
QVector2D texCoord;
QVector3D normal;
};
class GLObject
{
public:
GLObject();
VertexData *data;
GLushort *indices;
GLuint vboIds[2];
int faceCount, vertCount;
bool generateFromPLY(QString filename);
};
#endif // GLOBJECT_H
Here's the header for the other class.
#ifndef GEOMETRYENGINE_H
#define GEOMETRYENGINE_H
#include <QObject>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QVector2D>
#include <QVector3D>
#include <QFile>
#include <QDebug>
#include <QVector>
#include <globject.h>
class GeometryEngine : public QObject, protected QOpenGLFunctions
{
Q_OBJECT
public:
GeometryEngine();
~GeometryEngine();
void init();
void drawGeometry(QOpenGLShaderProgram *program);
//void drawCubeGeometry(QOpenGLShaderProgram *program);
bool generateFromPly(QString filename);
QVector<GLObject> drawables;
int drawableId = 0;
};
#endif // GEOMETRYENGINE_H
G:\Dropbox\GLSLDemo\globject.cpp:60: error: 'glGenBuffers' was not declared in this scope
glGenBuffers(2, vboIds);
along with the same error for the other gl calls.
Initially I had all the code in GeometryEngine to begin with. I didn't have a scope issue then. initilizeOpenGLFunctions() is called in GeometryEngine's init() if that's relevant.
^

QT OpenGL is weird. I think the QOpenGLFunctions maintains a common context. Also i'm bad at C++ and used protected wrong.

Related

Signal is emitting, connection returns true, slot not triggering

This is the second way I have rearranged this, and it is doing the same thing, so now I seek help. There are no errors being thrown, the slot just never does anything.
There are two other connections that are working between the same two cpps, and I decided to add this third one that the main window triggers, to the main window.
on_line_edit_returnPressed(), is printing hol_num and that is where it ends. ReadyHollander emits and HolPub never does anything. What am I doing wrong?
mainwindow cpp
#include "wheelscannerui.h"
#include "./ui_wheelscannerui.h"
gui_image_node *m_gui_image_node;
WheelScannerUI::WheelScannerUI(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::WheelScannerUI)
{
ui->setupUi(this);
connect(m_gui_image_node, &gui_image_node::ReadyImage, this, &WheelScannerUI::updateWheelImage);
connect(m_gui_image_node, &gui_image_node::OpenHollander, this, &WheelScannerUI::Open_No_ID);
connect(this, &WheelScannerUI::ReadyHollander, m_gui_image_node, &gui_image_node::HolPub);
qDebug() << connect(this, &WheelScannerUI::ReadyHollander, m_gui_image_node, &gui_image_node::HolPub);
ui->lineEdit->setVisible(false);
QMainWindow::showFullScreen();
}
WheelScannerUI::~WheelScannerUI()
{
delete ui;
}
void WheelScannerUI::Open_No_ID(QString qsteve)
{
ui->lineEdit->setVisible(true);
}
void WheelScannerUI::on_lineEdit_returnPressed()
{
QString hol_num = ui->lineEdit->text();
Q_EMIT ReadyHollander(hol_num);
ui->lineEdit->setVisible(false);
ui->lineEdit->clear();
qDebug() << hol_num;
}
main windows .h
#ifndef WHEELSCANNERUI_H
#define WHEELSCANNERUI_H
#include <QMainWindow>
#include <QtSql>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlQueryModel>
#include "gui_image_node.h"
#include <QPixmap>
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int64.h>
#include <sensor_msgs/image_encodings.h>
#include <nodelet/nodelet.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <pluginlib/class_list_macros.h>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
extern gui_image_node *m_gui_image_node;
QT_BEGIN_NAMESPACE
namespace Ui { class WheelScannerUI; }
QT_END_NAMESPACE
class WheelScannerUI : public QMainWindow
{
Q_OBJECT
public:
WheelScannerUI(QWidget *parent = nullptr);
~WheelScannerUI();
WheelScannerUI *m_WheelScannerUI;
void connectionClose()
{
db.close();
db.removeDatabase(QSqlDatabase::defaultConnection);
}
bool connectionOpen()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("~/wheels.db");
if(!db.open())
{
qDebug()<<("Database failed to open");
return false;
}
else{
qDebug()<<("database connected");
return true;
}
}
public slots:
Q_SLOT void Open_No_ID(QString qsteve);
Q_SLOT void open_db_table(); //trigger with a ros callback?
signals:
Q_SIGNAL void ReadyHollander(QString);
private slots:
void on_lineEdit_returnPressed();
void on_Mode_Button_clicked();
private:
Ui::WheelScannerUI *ui;
QSqlDatabase db;
};
#endif // WHEELSCANNERUI_H
node cpp
#include "gui_image_node.h"
gui_image_node::gui_image_node()
{
}
bool gui_image_node::init(int argc, char** argv)
{
m_pThread = new QThread();
this->moveToThread(m_pThread);
connect(m_pThread, &QThread::started, this, &gui_image_node::run);
ros::init(argc, argv, "wheels_gui_image");
if ( ! ros::master::check() )
{
return false;
}
ros::start();
ros::Time::init();
ros::NodeHandle nh;
hollander_pub = nh.advertise<std_msgs::String>("/hollander_chat", 1);
hol_trigger = nh.subscribe("awaiting_hollander", 1, &gui_image_node::Hollander_Screen_trigger_callback, this);
m_pThread->start();
return true;
}
void gui_image_node::Hollander_Screen_trigger_callback(const std_msgs::String::ConstPtr& msg)
{
std::string steve = msg->data;
QString qsteve = QString::fromStdString(steve);
Q_EMIT OpenHollander(qsteve);
}
void gui_image_node::HolPub(QString hol_num)
{
qDebug() << "received number";
std::string hol_num_conv = hol_num.toUtf8().constData();
std_msgs::String msg;
msg.data = hol_num_conv;
hollander_pub.publish(msg);
}
nodes .h
#ifndef GUI_IMAGE_NODE_H
#define GUI_IMAGE_NODE_H
#include <ros/ros.h>
#include <nodelet/nodelet.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <std_msgs/String.h>
#include <QMainWindow>
#include <QObject>
#include <QSharedDataPointer>
#include <QWidget>
#include <QThread>
#include <string>
#include <QMutex>
#include <QDebug>
#include <QDialog>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
class gui_image_node : public QThread
{
Q_OBJECT
public:
gui_image_node();
~gui_image_node();
bool init(int argc, char** argv);
void Hollander_Screen_trigger_callback(const std_msgs::String::ConstPtr& msg);
protected:
public slots:
Q_SLOT void run();
Q_SLOT void HolPub(QString hol_num);
signals:
Q_SIGNAL void OpenHollander(QString);
private:
ros::Publisher hollander_pub;
ros::Subscriber hol_trigger;
QThread * m_pThread;
};
#endif // GUI_IMAGE_NODE_H
OK, I figured it out, connect(this, &WheelScannerUI::ReadyHollander, m_gui_image_node, &gui_image_node::HolPub, Qt::Directconnection);
Since this is being triggered from another thread, Directconnection allows the thread it is being emitted from to trigger the slot in a different thread.

why the first included class is recognized but the second included class is not? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Hi I have 2 classes named Search and Search2. When I include them in the mainwindow.h in qt, only the first included class is recognized and the second one is not. In other words if I first include search2.h then search.h, it will not recognize Search. These are my header files. Can anyone help?
mainwndow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui/QWidget>
#include "ui_mainwindow.h"
#include "search.h"
#include <QString>
#include <QStringList>
#include <QThread>
#include "search2.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QThread* thread1 ;
QThread* thread2 ;
Search* mySearch;
Search2* mySearch2;
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
float frequency;
QString freq ;
int flag;
signals:
private slots:
void on_pushButton_2_clicked();
void receive_from_object(QStringList);
void receive_from_object1(QStringList);
void receive_from_object2(QStringList);
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
search.h
#ifndef SEARCH_H
#define SEARCH_H
#include <QWidget>
#include <QObject>
#include <QMutex>
#include <QString>
#include <QStringList>
#include <QDebug>
#include <qtimer.h>
#include <QtConcurrentRun>
#include <QMessageBox>
#include <QThread>
#include <QMessageBox>
#include <QWaitCondition>
namespace Ui {
class Search;
}
class Search : public QWidget
{
Q_OBJECT
public:
explicit Search(QWidget *parent = 0);
void Input_param(float RFfreq);
~Search();
void requestWork();
void abort();
QStringList slist3;
QString freqLog;
double output_search[5] ;
private:
Ui::Search *ui;
bool _abort;
bool _working;
signals:
void transmit_to_gui(QStringList);
void finished();
void workRequested();
void frequencyFound(QStringList);
void frequencyLog(QString) ;
void finishedscan() ;
public slots:
void receiveDateBand(QString);
};
#endif // SEARCH_H
search2.h
#ifndef SEARCH_H
#define SEARCH_H
#include <QWidget>
#include <QObject>
#include <QMutex>
#include <QString>
#include <QStringList>
#include <QDebug>
#include <qtimer.h>
#include <QtConcurrentRun>
#include <QMessageBox>
#include <QThread>
#include <QMessageBox>
#include <QWaitCondition>
namespace Ui {
class Search2;
}
class Search2 : public QWidget
{
Q_OBJECT
public:
explicit Search2(QWidget *parent = 0);
~Search2();
void requestWork2();
void abort2();
QStringList slist3;
QString freqLog;
double output_search[5] ;
private:
Ui::Search2 *ui;
bool _abort;
bool _working;
//QMutex mutex;
signals:
void transmit_to_gui1(QStringList);
void transmit_to_gui2(QStringList);
void finished2();
void workRequested2();
void frequencyFound(QStringList);
void frequencyLog(QString) ;
void finishedscan2() ;
public slots:
void receiveDateBand2(QString) ;
};
#endif // SEARCH_H
Thanks in advance.
Now excatly sure if this is the reason, but in your second class you added the same header definition:
#ifndef SEARCH_H instead of #ifndef SEARCH2_H

Qt Bluetooth Peripheral Segmentation Fault

I'm trying to advertise a Qt Bluetooth class in peripheral mode using the code given in this example directly from the Qt Documentation Page.
I'm using Linux(Kubuntu 18.0.1) with C++ code. My Qt version is 5.7 with bluez version 5.48-0ubuntu3.1
My code looks like this the following:
bleHololenseServer.h:
#ifndef BLEHOLOLENSESERVER_H
#define BLEHOLOLENSESERVER_H
#include <QDebug>
#include <QObject>
#include <QLowEnergyAdvertisingData>
#include <QLowEnergyAdvertisingParameters>
#include <QLowEnergyServiceData>
#include <QLowEnergyCharacteristicData>
#include <QLowEnergyDescriptorData>
#include <QLowEnergyController>
#include <QLowEnergyService>
/*
* Visualization Class for the AnkiCar and street
*/
class BLEHololenseServer : public QObject{
Q_OBJECT
public:
BLEHololenseServer();
void startAdvertisting();
private:
signals:
public slots:
};
#endif // BLEHOLOLENSESERVER_H
bleHololenseSever.cpp
#include <QDebug>
#include <QLowEnergyAdvertisingData>
#include <QLowEnergyAdvertisingParameters>
#include <QLowEnergyServiceData>
#include <QLowEnergyCharacteristicData>
#include <QLowEnergyDescriptorData>
#include <QLowEnergyController>
#include <QLowEnergyService>
#include "../headers/bleServer.h"
#include <QScopedPointer>
BLEHololenseServer::BLEHololenseServer(){
}
void BLEHololenseServer::startAdvertisting(){
QLowEnergyAdvertisingData advertisingData;
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName("AnkiServer");
advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
QLowEnergyCharacteristicData charData;
charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
charData.setValue(QByteArray(2, 0));
charData.setProperties(QLowEnergyCharacteristic::Notify);
const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration, QByteArray(2, 0));
charData.addDescriptor(clientConfig);
QLowEnergyServiceData serviceData;
serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
serviceData.setUuid(QBluetoothUuid::HeartRate);
serviceData.addCharacteristic(charData);
const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
const QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
const QLowEnergyAdvertisingParameters advertisingParameters = QLowEnergyAdvertisingParameters();
leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);
}
I create this class in the a QObject class called created in main.cpp and when i call the startAdertising function, i get an Segmentationfault the second i call the last line in my startAdvertising function:
leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);
Backtracing the error in gdb gives me something like this:
#0 0x00007ffff71fb082 in ?? () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#1 0x00007ffff71b56b7 in QLowEnergyController::~QLowEnergyController() () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#2 0x00007ffff71b56d9 in QLowEnergyController::~QLowEnergyController() () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#3 0x000055555557eb4e in QScopedPointerDeleter<QLowEnergyController>::cleanup (pointer=0x5555557b78b0) at ../../../../Qt/5.7/gcc_64/include/QtCore/qscopedpointer.h:60
#4 0x000055555557eaa3 in QScopedPointer<QLowEnergyController, QScopedPointerDeleter<QLowEnergyController> >::~QScopedPointer (this=0x7fffffffe108, __in_chrg=<optimized out>)
at ../../../../Qt/5.7/gcc_64/include/QtCore/qscopedpointer.h:107
#5 0x000055555557e8a2 in BLEHololenseServer::startAdvertisting (this=0x5555557b7aa0) at src/bleHololense/bleHololenseServer.cpp:35
#6 0x000055555556abab in DriveMode::DriveMode (this=0x5555557b2940, parent=0x0) at src/drivemode.cpp:87
#7 0x000055555555ccdb in main (argc=1, argv=0x7fffffffe448) at src/main.cpp:34
}
Try not to use QScopedPointer.
This code works like a charm for me:
class header:
private:
QLowEnergyController* m_controller;
source:
constructor
m_controller = QLowEnergyController::createPeripheral(this);
class::startAdvertisting() method
QLowEnergyCharacteristicData l_characteristic_data;
l_characteristic_data.setUuid(QBluetoothUuid::TemperatureMeasurement);
l_characteristic_data.setValue(QByteArray(2, 0));
l_characteristic_data.setProperties(QLowEnergyCharacteristic::Notify);
const QLowEnergyDescriptorData l_config(QBluetoothUuid::ClientCharacteristicConfiguration,
QByteArray(2, 0));
l_characteristic_data.addDescriptor(l_config);
QLowEnergyServiceData l_temp_data;
l_temp_data.setType(QLowEnergyServiceData::ServiceTypePrimary);
l_temp_data.setUuid(QBluetoothUuid::Temperature);
l_temp_data.addCharacteristic(l_characteristic_data);
QLowEnergyService* l_temp_service = m_controller->addService(l_temp_data, this);
QLowEnergyAdvertisingData l_advertising_data;
l_advertising_data.setLocalName("FakeBle");
l_advertising_data.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::Temperature);
m_controller->startAdvertising(QLowEnergyAdvertisingParameters(), l_advertising_data, l_advertising_data);

Slot is not detected when QPushbutton is released [duplicate]

This question already has answers here:
QObject connection function
(3 answers)
Closed 4 years ago.
I am currently making a main menu for a game, but when the button is clicked it does not seem to call the relevant slot (The button does not do anything). I have tried to move the button to a thread to ensure that nothing is keeping it from running immediately. I have written a simpler program where the button works, so it would seem the problem is not with the button or signal itself. Therefore it must be something keeping it from running?
The source file is given as:
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QObject>
#include <QBrush>
#include"game.h"
#include <QMediaPlayer>
#include <QImage>
#include<QGraphicsPixmapItem>
#include <QPixmap>
#include<QGraphicsItem>
#include <QPushButton>
#include <QLineEdit>
#include <QPalette>
#include <QMessageBox>
#include <QLabel>
#include <QComboBox>
Game::Game(QWidget*parent)
{
//adding the background
QGraphicsScene *scene=new QGraphicsScene();//to create a new graphics scene
scene->setSceneRect( 0,0,1366,768);
setBackgroundBrush(QBrush(QImage(":/images/Back_1_withlogo.jpg")));
setScene(scene);
show();
setFixedSize(1366,768);
//adding logo
//setPixmap(QPixmap(":/images/Logo.png"));
//adding the soundtrack
music=new QMediaPlayer();
music->moveToThread(&MusicThread);
music->setMedia(QUrl("qrc:/sounds/Surreal-Game-Menu.mp3"));
music->play();
MusicThread.start();
//add GUI
this->setFixedSize(1366,768);
this->move(-7,-5);
//start button and username edit box
btnStart = new QPushButton(this);
btnStart->moveToThread(&btnThread);
UserName = new QLineEdit(this);
UserName->setFixedSize(100,25);
UserName->move(300,300);
UserName->show();
UserName->setMaxLength(12);
btnStart->setText("Start");
btnStart->setFixedSize(100,30);
btnStart->show();
btnStart->move(300,600);
connect(btnStart, SIGNAL (released()),this, SLOT (btnStart_clicked()));
btnThread.start();
//label for username edit box
QLabel *LblUsername= new QLabel(this);
LblUsername->setStyleSheet("QLabel { color : white; }");
LblUsername->setFixedSize(100,30);
LblUsername->move(230,300);
LblUsername->setText("Username:");
LblUsername->show();
QLabel *lblGameMode = new QLabel(this);
lblGameMode->setStyleSheet("QLabel { color : white; }");
lblGameMode->setFixedSize(100,30);
lblGameMode->move(190,450);
lblGameMode->setText("Select Game mode:");
lblGameMode->show();
//combobox to select players
GameMode=new QComboBox(this);
GameMode->move(300,450);
GameMode->setFixedSize(100,30);
QStringList Modelist=(QStringList()<<"singleplayer"<<"co-op"<<"multiplayer (3 players)");
GameMode->addItems(Modelist);
GameMode->show();
}
void Game::btnStart_clicked()
{
if (UserName->text()==NULL)
{
QMessageBox messageBox;
messageBox.critical(0,"Error","Please insert username!");
messageBox.setFixedSize(500,200);
}
else
{
numPlayers = GameMode->currentIndex();
SUsername= UserName->text();
UserName->setText("works");
}
}
And the header code is given as:
#ifndef GAME_H
#define GAME_H
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QMediaPlayer>
#include <QThread>
#include <QComboBox>
class Game:public QGraphicsView
{
public:
Game(QWidget*parent=0);
QGraphicsScene*scene;
private slots:
void btnStart_clicked();
private:
QPushButton *btnStart;
QLineEdit *UserName;
QMediaPlayer *music;
QThread MusicThread;
QString SUsername;
QThread btnThread;
QComboBox *GameMode;
int numPlayers;
};
#endif // GAME_H
I received the following error:
QObject::connect: No such slot QGraphicsView::btnStart_clicked() in game.cpp:57
add a Q_OBJECT macro as shown below:
class Game:public QGraphicsView
{
Q_OBJECT
public:
Game(QWidget*parent=0);
QGraphicsScene*scene;
private slots:
void btnStart_clicked();
private:
QPushButton *btnStart;
QLineEdit *UserName;
QMediaPlayer *music;
QThread MusicThread;
QString SUsername;
QThread btnThread;
QComboBox *GameMode;
int numPlayers;
};
This macro helps compiler to determine that the class is using signals and slots mechanism. Basically it creates a entry of defined signals and slots in MOC (meta object compiler) file.
Go through below link for info:
http://www.bogotobogo.com/Qt/Qt5_Q_OBJECT_Macro_Meta_Object.php
QObject::connect (btnStart, &QPushButton::clicked, this, &Game::btnStart_clicked); might work... if you want to use the release event try overriding virtual void mouseRelease or the eventFilter.
And of course... the Q_OBJECT marco.

Crash when adding QGraphicsProxyWidget

I'm trying to have a QPushButton in my scene, but when I'm trying to add the QGraphicsProxyWidget to the scene, it crashes.
So here's the .cpp:
#include "upgradecromagnon.h"
#include "game.h"
#include <QGraphicsProxyWidget>
#include <qDebug>
extern Game *game;
UpgradeCromagnon::UpgradeCromagnon()
{
this->setRect(-50,0,150,50);
buttonAmelio = new QPushButton("salut");
teste();
}
void UpgradeCromagnon::teste()
{
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(buttonAmelio);
scene()->addItem(proxy);
}
and its .h:
#ifndef UPGRADECROMAGNON_H
#define UPGRADECROMAGNON_H
#include <QPainter>
#include <QGraphicsRectItem>
#include <QPushButton>
class UpgradeCromagnon: public QGraphicsRectItem
{
public:
UpgradeCromagnon();
void teste();
private:
QPushButton *buttonAmelio;
};
#endif // UPGRADECROMAGNON_H
Your UpgradeCromagnon constructor calls UpgradeCromagnon::teste which, in turn, calls QGraphicsItem::scene. At that point QGraphicsItem::scene must return a null pointer since there's no possible way the UpgradeCromagnon instance can have been added to a QGraphicsScene before its contructor has completed (not according to the code you've provided at any rate).

Resources