Error :No match for operator ">>" in Qt - c++

I have written a client GUI to receive structure from server. When I compile, I receive the error:
error: no match for 'operator>>' in 'is >>
newstruct.basestruct::element1'
Can you point me where I have gone wrong?
dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
#include <QString>
#include <QDebug>
#include <QTextStream>
#include <QDataStream>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->pushButton->setText("Connect");
ui->pushButton_2->setText("Ok");
ui->pushButton_3->setText("Close");
ui->pushButton_4->setText("Disconnect");
}
Dialog::~Dialog()
{
delete ui;
}
QDataStream& operator >>(QDataStream& is,const basestruct& newstruct)
{
is >> newstruct.element1;
qDebug()<<newstruct.element1;
}
void Dialog::Read()
{
socket->waitForReadyRead(-1);
QDataStream receiveData(socket);
receiveData.setVersion(QDataStream::Qt_4_0);
receiveData >>newstruct ;
}
void Dialog::on_pushButton_clicked()
{
socket=new QTcpSocket(this);
socket->connectToHost("127.0.0.1",5000);
if (socket->waitForConnected(-1))
qDebug()<<"Connected";
Read();
}
void Dialog::on_pushButton_4_clicked()
{
socket->close();
qDebug()<<"Disconnected";
}
dialog.h:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QTcpSocket>
namespace Ui {
class Dialog;
}
struct basestruct
{
int element1;
int element2;
};
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void Read();
private slots:
void on_pushButton_clicked();
void on_pushButton_4_clicked();
private:
Ui::Dialog *ui;
QTcpSocket *socket;
friend Q_CORE_EXPORT QDataStream &operator >>(QDataStream&,const struct basestruct&);
struct basestruct newstruct;
};
#endif // DIALOG_H

Related

Implement function to QT Button

I cant find any solution for the problem. I created an ui with qt and wanted to implement a function for the button. It works if i delete the buttons in the header file.
Error
/usr/bin/ld: CMakeFiles/QT3.dir/QT3_autogen/mocs_compilation.cpp.o: in function MainWindow::qt_metacall(QMetaObject::Call, int, void**)': mocs_compilation.cpp:(.text+0x10a): undefined reference to MainWindow::on_nextStep_clicked()'
/usr/bin/ld: CMakeFiles/QT3.dir/QT3_autogen/mocs_compilation.cpp.o: in function MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)': mocs_compilation.cpp:(.text+0x41): undefined reference to MainWindow::on_nextStep_clicked()'
Header File
#ifndef MAINWINDOW3_H_
#define MAINWINDOW3_H_
#include <QMainWindow>
#include <opencv2/highgui/highgui.hpp>
#include <QGraphicsScene>
//QT_BEGIN_NAMESPACE
//namespace Ui { class MainWindow; }
//QT_END_NAMESPACE
namespace Ui {
class MainWindow;
}
class MainWindow: public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
private slots:
// void on_pushButton_5_clicked(); // add Wall
// void on_pushButton_clicked(); // oben
// void on_pushButton_2_clicked(); // links
// void on_pushButton_3_clicked(); // rechts
// void on_pushButton_4_clicked(); // unten
void on_pushButton_clicked();
void on_nextStep_clicked();
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
protected:
// bool eventFilter(QObject *, QEvent *);
};
#endif /* MAINWINDOW3_H_ */
Cpp File
#include "mainwindow3.h"
#include "ui_mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QDebug>
#include <QMouseEvent>
#include <QObject>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
// ui->graphicsView->installEventFilter(this);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::on_pushButton_clicked() {
std::cout << "add btn clicked()" << std::endl;
}
a slot is just another method in Qt for asyn events
if you define in the header:
void on_nextStep_clicked();
then you need a implementation in the cpp:
void MainWindow::on_nextStep_clicked()
{
qDebug() << "Hallo Welt!";
qDebug() << "on_nextStep_clicked()";
}

Example SLOT/SIGNAL between two object QT

My app, consists in 2 different object (QObject and QMainWIndow), and I am wondering how to communicate between them with SLOT/SIGNAL. Moreover, does existing better approach ?
Can someone make an simple example ? Thank :)
sample
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
#include "object.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "object.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));
}
MainWindow::~MainWindow()
{
delete ui;
}
object.h
#ifndef OBJET_H
#define OBJET_H
#include "mainwindow.h"
#include <QMainWindow>
#include <QObject>
class Object : public QObject
{
Q_OBJECT
public:
explicit Object(QObject *parent = 0);
bool state;
signals:
private slots:
void chkState(Ui::MainWindow *ui);
};
#endif // OBJET_H
objet.cpp
#include "object.h"
#include "mainwindow.h"
Object::Object(QObject *parent) : QObject(parent)
{
}
void Object::chkState(Ui::MainWindow *ui)
{
if (ui->chkState->isChecked())
{
ui->state->setText("true");
state = true;
}
else
{
ui->state->setText("false");
state = false;
}
}
Here is a simple example of how to emit signals and slots.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "object.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void transmit_to_object(bool value);
private slots:
void receive_from_object(bool value);
void on_checkBox_clicked();
private:
Ui::MainWindow *ui;
object m_object;
};
#endif // MAINWINDOW_H
#ifndef OBJECT_H
#define OBJECT_H
#include <QObject>
class object : public QObject
{
Q_OBJECT
public:
explicit object(QObject * parent = 0);
signals:
void transmit_to_gui(bool value);
private slots:
void receive_from_gui(bool value);
private:
bool state;
};
#endif // OBJECT_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&m_object,SIGNAL(transmit_to_gui(bool)),this,SLOT(receive_from_object(bool)));
connect(this,SIGNAL(transmit_to_object(bool)),&m_object,SLOT(receive_from_gui(bool)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::receive_from_object(bool value)
{
if(value)
{
ui->lineEdit->setText("true");
}
else
{
ui->lineEdit->setText("false");
}
}
void MainWindow::on_checkBox_clicked()
{
if(ui->checkBox->isChecked())
{
emit transmit_to_object(true);
}
else
{
emit transmit_to_object(false);
}
}
#include "object.h"
#include "mainwindow.h"
object::object(QObject *parent)
{
}
void object::receive_from_gui(bool value)
{
state = value;
emit transmit_to_gui(state);
}
There are several errors in your code.
First:
connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));
Here you say:
"when we click on the ui->chkState, I want you call a function in this, which is the object->chkState slot". That's definitly not what you want.
What is object ? this object hasn't been created. What you want is :
connect(ui->chkState, SIGNAL(clicked()), myobject, SLOT(chkState()));
with myobject an object of type Object so you need to add in your mainwindow.h a
Object *myobject;
and in your mainwindow.cpp before the connect:
myobject = new Object(this);
Moreover, your function void chkState(Ui::MainWindow *ui); won't work because you cannot get the mainwindow ui in parameter like that.
What I advise you to do, if it's only for tests so you know that parent is the type of MainWindow, you can do:
void Object::chkState()
{
MainWindow* parent = static_cast<MainWindow*>(parent());
if (parent->ui->chkState->isChecked())
{
parent->ui->state->setText("true");
state = true;
}
else
{
parent->ui->state->setText("false");
state = false;
}
}
So the parameter in your slot is removed.

developing an application that display an image on a label(control) and after click that image the new image will display in other label (control)

i am working on QT C++,developing an application that display an image on a label(control) and after click that image the new image will display in other label (control). i create a new project: application->QtWidget Application. then i select class Qdialog. in project file are
- my_qlabel_mouseEvent.pro
and in header folder are
- dialog.h
- my_qlabel.h
and then source folder has files
1: dialog.cpp
2:main.cpp
3: my_qlabel.cpp
and then in form folder has
1: dialog.cpp.
when i add the new class my_qlabel with base class QLabel, type information: QWidget after creating this the setPixmap does not work. its show error the error is that "in constructor 'Dialog::Dialog(QWidget*)' class my_qlabel has no member named setPixmap" please solve this error or reffer me any site or other sources.
**my_qlabel.cpp**
#include "my_qlabel.h"
#include <QPixmap>
my_qlabel::my_qlabel(QWidget *parent) : QWidget(parent)
{
}
void my_qlabel::mouseMoveEvent(QMouseEvent *)
{
emit Mouse_Pos();
}
void my_qlabel::mousePressEvent(QMouseEvent *)
{
emit Mouse_Pressed();
}
void my_qlabel::leaveEvent(QEvent *)
{
emit Mouse_Left();
}
**main.cpp**
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
**dialog.cpp**
#include "dialog.h"
#include "ui_dialog.h"
#include "my_qlabel.h"
#include <QPixmap>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QPixmap pix("E:/osld/andgate.png");
ui->lblMouse->setPixmap(pix);
connect(ui->lblMouse, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
connect(ui->lblMouse, SIGNAL(Mouse_Pos()), this, SLOT(Mouse_current_pos()));
connect(ui->lblMouse, SIGNAL(Mouse_Left()), this, SLOT(Mouse_left()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::Mouse_current_pos()
{
ui->lblMouse_Current_Event->setText("Mouse Moving");
}
void Dialog::Mouse_Pressed()
{
ui->lblMouse_Current_Event->setText("Mouse Pressed");
}
void Dialog::Mouse_left()
{
ui->lblMouse_Current_Event->setText("Mouse Left");
}
**dialog.h**
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void Mouse_current_pos();
void Mouse_Pressed();
void Mouse_left();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
**my_qlabel.h**
#ifndef MY_QLABEL_H
#define MY_QLABEL_H
#include <QWidget>
#include <QMouseEvent>
#include <QEvent>
#include <QDebug>
#include <QPixmap>
class my_qlabel : public QWidget
{
Q_OBJECT
public:
explicit my_qlabel(QWidget *parent = 0);
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void leaveEvent(QEvent *);
signals:
void Mouse_Pressed();
void Mouse_Pos();
void Mouse_Left();
public slots:
};
#endif // MY_QLABEL_H

Qt creating a class and using in a vector

I'm new in Qt and C++. I want to implement a vector with using a class called Item. I think I'm doing a beginners mistake. I'm getting the below error. Can someone please help?
C:\Qt\Qt5.6.1\5.6\msvc2015_64\include\QtCore\qvector.h:631: error: C2280: 'Item::Item(const Item &)': attempting to reference a deleted function
Below is my mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
#include <QtCore>
#include <QtGui>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::AddRoot(QString item)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);
itm->setText(0,item);
ui->treeWidget->addTopLevelItem(itm);
}
void MainWindow::AddChild(QTreeWidgetItem *parent,QString item)
{
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
Item item;
item.setDescription(ui->lineEdit->text());
data.push_back(item);
ui->treeWidget->setColumnCount(1);
AddRoot(item.getDescription());
}
And my item.cpp class
#include "item.h"
#include <QtCore>
#include <QtGui>
Item::Item()
{
}
void Item::setDescription(QString desc)
{
Description = desc;
}
void Item::setEnterDate(QDateTime enterDate)
{
EnterDate = enterDate;
}
void Item::setEndDate(QDateTime endDate)
{
EndDate = endDate;
}
QString Item::getDescription()
{
return Description;
}
QDateTime Item::getEnterDate()
{
return EnterDate;
}
QDateTime Item::getEndDate()
{
return EndDate;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <item.h>
#include <QtGui>
#include <QtCore>
#include <QTreeWidget>
mainwindow.h:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QVector<Item> data;
void AddRoot(QString item);
void AddChild(QTreeWidgetItem *parent,QString item);
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
item.h:
#ifndef ITEM_H
#define ITEM_H
#include <QtCore>
#include <QtGui>
class Item : public QObject
{
Q_OBJECT
public:
explicit Item(QObject *parent = 0);
QString Description;
QDateTime EnterDate;
QDateTime EndDate;
QString getDescription();
QDateTime getEnterDate();
QDateTime getEndDate();
void setDescription(QString desc);
void setEnterDate(QDateTime enterDate);
void setEndDate(QDateTime endDate);
};
#endif // ITEM_H
Item is a QObject. QObjects are know for not having a copy constructor. In order to use the QVector, the item needs to have a copy constructor. Either change Item to not being a QObject, or use QSharedPointer (but only if you understand ownership).

display is not showing in text box from other class in qt

I have made made a DDisplay class to display my data on gui text box but I have some strange situation, when I call the function from mainwindow class it is working fine but when I try to call the same function from other class it is not working. below is the small code my program
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <ddisplay.h>
#include <QMainWindow>
#include "test.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
DDisplay b;
Test c;
private slots:
void display(const QString &a);
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(&b,SIGNAL(display(QString)),this,SLOT(display(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::display(const QString &a){
ui->textBrowser->setText(a);
}
void MainWindow::on_pushButton_clicked()
{
b.printf("hello"); //this function working fine
//c.printf("lllll"); //not working
}
test.h
#ifndef TEST_H
#define TEST_H
#include <QString>
#include <ddisplay.h>
class Test
{
public:
Test();
void printf(const QString &a);
DDisplay b;
};
test.cpp
#include "test.h"
Test::Test()
{
}
void Test::printf(const QString &a){
b.printf(a);
}
ddisplay.h
#include
class DDisplay : public QObject
{
Q_OBJECT
public:
explicit DDisplay(QObject *parent = 0);
void printf(const QString &a);
QString b;
signals:
void display(const QString &a);
public slots:
};
ddisplay.cpp
#include "ddisplay.h"
DDisplay::DDisplay(QObject *parent) :
QObject(parent)
{
}
void DDisplay::printf(const QString &a)
{
b+=a;
emit display(b);
}
c.printf("lllll"); //not working
It's not working because you don't have a connect for this object. DDisplay in Test class is a dfferent instance than that in a MainWindow.
One possible solution for your question in comments below is to connect DDisplay's signal and MainWindow's slot either in main.cpp or in specialized initialization class.