Facing some problem with global Variable in QT C++ - sqlite

I have a login page and a welcome page (which open if user can bypass the login). In my login page
there is a line edit for entering username. So, for holding the username I declared a global variable in
the login.h.
My login.h file:
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class login : public Qlogin
{
Q_OBJECT
public:
login(QWidget *parent = nullptr);
~login();
private:
Ui::login *ui;
QString userDat; ///global variable
};
#endif // MAINWINDOW_H
I hold the username from the line edit in login.cpp like this.
My login.cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
login::login(QWidget *parent)
: Qlogin(parent)
, ui(new Ui::login)
{
ui->setupUi(this);
userDat = ui-> login_lineEdit ->text();
}
login::~login()
{
delete ui;
}
I want to use the global variable userDat in my welcome.cpp file like this.
ui-> welcome_lineEdit->setText(userDat);
I've included all required header file in welcome.h and welcome.cpp. But it still says:
userDat is not declared in this scope.
please help

Update from comments:
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class UserData {
public:
static QString userName;
};
class login : public Qlogin
{
Q_OBJECT
public:
login(QWidget *parent = nullptr);
~login();
private:
Ui::login *ui;
};
#endif // MAINWINDOW_H
in login.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
QString UserData::userName = "empty";
login::login(QWidget *parent)
: Qlogin(parent)
, ui(new Ui::login)
{
ui->setupUi(this);
UserData::userName = ui-> login_lineEdit ->text();
}
login::~login()
{
delete ui;
}
and somethere in another place:
ui-> welcome_lineEdit->setText(UserData::userName);

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.

QlineEdit: No such file or directory

I have a problem. I have QT 5.3.1 and Ubuntu 14.04.
For example:
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QPixmap>
#include <QImage>
#include <QComboBox>
#include <QFileDialog>
#include <QlineEdit>
#include <QLabel>
#include <QFile>
#include <QIODevice>
#include <QSlider>
#include <QMessageBox>
Works well.
The problem is the following line:
#include <QlineEdit>
Compilator return error: QlineEdit: No such file or directory.
How do I fix this?
There is no such class a QlineEdit. However, there is a QLineEdit class.
Note the capital 'L' in Line.
As you've tagged your question with 'Ubuntu' and you're using Linux, it's likely to be case-sensitive, so the capitalisation matters here.

Qt QOpenGLFunctions not declared in this scope

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.

Qt - ERROR: ISO C++ forbids declaration of ‘MainWindow’ with no type

First of all, i posted the question after a lot of searching, reading and debugging ... but can't solve the problem.
I have two class MainWindow and Controller, both of them need to include the other. I tried to solve the problem of recursive include using forwarding, but it didn't work.
I think i have some problem with implementing forward with namespaces.
The code for both classes: (i tried to shorten the code as much as possible)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include "controller.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void addRadioButtons();
void changeViewQML(QString);
void changeViewXML(QString);
private:
Ui::MainWindow *ui;
controller controllerObj;
};
#endif // MAINWINDOW_H
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QtCore>
#include "mainwindow.h"
class controller : public QThread
{
public:
controller();
void response_handler(QString);
private:
MainWindow *viewObj;
protected:
void run();
};
#endif // CONTROLLER_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;
}
void MainWindow::changeViewQML(QString s)
{
qDebug(s.toAscii());
}
controller.cpp
#include "controller.h"
controller::controller()
{
}
void controller::response_handler(QString responseFilePath)
{
viewObj->changeViewQML(responseFilePath);
}
and i keep having this ISO error.
sorry for the long code, and if any part is not clear enough
just forward declare the MainWindow. Replace
#include "mainwindow.h" in the controller.h
with
namespace Ui {
class MainWindow;
}
also refer to it as Ui::MainWindow *viewObj; or write using namespace Ui in your controller.h
EDIT
this compiles fine:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include "controller.h"
namespace Ui{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void addRadioButtons();
void changeViewQML(QString);
void changeViewXML(QString);
private:
Ui::MainWindow *ui;
controller controllerObj;
};
#endif // MAINWINDOW_H
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QtCore>
//#include "mainwindow.h"
class MainWindow;
class controller : public QThread
{
public:
controller();
void response_handler(QString);
private:
MainWindow *viewObj;
protected:
void run();
};
#endif // CONTROLLER_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;
}
void MainWindow::changeViewQML(QString s)
{
qDebug(s.toAscii());
}
controller.cpp
#include "controller.h"
#include "mainwindow.h"
controller::controller()
{
}
void controller::run()
{
}
void controller::response_handler(QString responseFilePath)
{
viewObj->changeViewQML(responseFilePath);
}

how to get number of the current position of the cursor (qt)

I need small help, I'm using class Qslider from qt, how can I get the number of the current position of the cursor (with which function I can do that) thanks in advance
edited
I want to implement one thing: when I reach the max of the interval I want to quit, how can I do this with slot and signal?
I am assuming you want the value of the slider?
int QSlider::value ()
I looked at the code in your other post and this is what I came up with after cleaning it up:
main.cpp
#include <QApplication>
#include "mywidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QObject>
include <QPushButton>
#include <QSlider>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
~MyWidget(){};
public slots:
void getSliderValueAndQuit();
private:
QPushButton *quit;
QSlider *slider;
};
#endif // MYWIDGET_H
myWidget.cpp
#include "mywidget.h"
#include <QWidget>
#include <QObject>
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));
QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);
slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(slider, SIGNAL(valueChanged(int)),lcd, SLOT(display(int)));
connect(slider,SIGNAL(sliderReleased()), SLOT(getSliderValueAndQuit()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
void MyWidget::getSliderValueAndQuit(){
if(slider->value() == slider->maximum())
close();
}

Resources