Solved Connect QLineEdit with my class
-
Hello,
I have been written a class, which creates an object for a QGraphicsView. In the class are different functions, like timer, movement and other. The class is stored in a seperate file (C++ source code and C++ header file).
Additional I have a form besides my mainform. In this form is located the QGraphicsView and a QLineEdit.
Now I would like to connect the SIGNAL on_lineEdit_returnPressed() with my class.
Because I create several instances of my class, I think I have to connect the QLineEdit in my class.Right now I tried this:
connect(GameWindow.ui->input_edit(),SIGNAL(returnPressed()),this,SLOT(checkResult()));
GameWindow is my form with the QLineEdit (input_edit) and the QGraphicsView
checkResult is inside my class (public slot)When I try to compile everything I get this error:
/home/tce/CTux/mytextitem.cpp:18: error: expected primary-expression before ‘.’ token connect(GameWindow.ui->input_edit(),SIGNAL(returnPressed()),this,SLOT(checkResult()));
Where is the error in my thinking? What do I have to do to solve the problem?
I hope somebody can help me
Tobias
-
@navyseabear said in Connect QLineEdit with my class:
connect(GameWindow.ui->input_edit(),SIGNAL(returnPressed()),this,SLOT(checkResult()));
This is wrong, remove () after input_edit
-
@navyseabear said in Connect QLineEdit with my class:
/home/tce/CTux/mytextitem.cpp:18: error: expected primary-expression before ‘.’ token
still the same error:
/home/tce/CTux/mytextitem.cpp:18: error: expected primary-expression before ‘.’ token
What do I do wrong??
Tobias
-
try:
connect(ui->input_edit,SIGNAL(returnPressed()),this,SLOT(checkResult())); -
now I have
error: ‘ui’ was not declared in this scope
I have both files
#include "gamewindow.h" #include "ui_gamewindow.h"
added. So I should work. Or do I make something wrong in my thinking?
Tobias
-
Are you inside
gamewindow.cpp
? -
No, inside my class (mytextitem).
I thought I should put it there, because I create several instances of my class during runtime.
Tobias
-
Hi and welcome to devnet,
You can't connect a class. You need an object instance for that.
-
Hmm, and how do I do that?
I'm really new in C++ and Qt. Normaly I use Pascal and VB.Tobias
-
@navyseabear said in Connect QLineEdit with my class:
Hmm, and how do I do that?
You should already have an instance of GameWindow somewhere in your app. How else would you use it?
Also it is bad praxis to access private data of a class (like ui) from other classes. You should do this connection from GameWindow not from mytextitem. -
OK, so far I understood that.
My problem is just that I have created the class, because I create there individual objects, which then walk independently on my QGraphicView.
Using the QLineEdit, each individual object should now check whether the input for the object is correct and then destroy itself or execute a corresponding routine.
Probably I have not quite understood this with the SIGNAL / SLOTS, but how else should I distribute the input of the QLineEdit to all objects so that they then compare the value? I do not know how many objects are created at runtime.Tobias
-
Do you mean that you have many QGraphicsItem objects in your scene that you want to do a check on based on the content of the QLineEdit ?
-
Yes, and I do not know how to connect them to the QLineEdit.
Tobias
-
@navyseabear
Hi
Where you create the new mytextitem, there you would hook it up.
Pr mytextitem.
Hopefully its same class as where the UI lives. -
Here is my code:
gamewindow.h
#ifndef GAMEWINDOW_H #define GAMEWINDOW_H #include <QGraphicsScene> #include <QWidget> #include "mytextitem.h" namespace Ui { class GameWindow; } class GameWindow : public QWidget { Q_OBJECT public: explicit GameWindow(QWidget *parent = 0); ~GameWindow(); private slots: void on_pushButton_2_clicked(); private: Ui::GameWindow *ui; QGraphicsScene *scene; MyTextItem *textitem; int timerID; protected: void timerEvent(QTimerEvent *event); void on_lineEdit_returnPressed(); }; #endif // GAMEWINDOW_H
gamewindow.cpp
#include "gamewindow.h" #include "ui_gamewindow.h" #include "mytextitem.h" #include <QGraphicsTextItem> #include <QGraphicsScene> GameWindow::GameWindow(QWidget *parent) : QWidget(parent), ui(new Ui::GameWindow)private slots: void on_pushButton_2_clicked(); { ui->setupUi(this); this->setWindowState(Qt::WindowMaximized); scene = new QGraphicsScene(this); } GameWindow::~GameWindow() { delete ui; } void GameWindow::on_pushButton_2_clicked() { int Xmax; int Ymax; float Scale; Xmax = ui->graphicsView->width()-10; Ymax = ui->graphicsView->height()-10; Scale = (float)Xmax / (float)800; if (Scale < 1) { Scale = 1; } Xmax = Xmax / Scale; Ymax = Ymax / Scale; scene->setSceneRect(0,0,Xmax,Ymax); QTransform transform; transform.scale(Scale,Scale); ui->graphicsView->setTransform(transform); ui->graphicsView->update(); ui->graphicsView->setScene(scene); textitem = new MyTextItem("TEST",80); textitem->setX_Max(Xmax); textitem->setY_Max(Ymax); textitem->setSpeed(100); scene->addItem(textitem); } void GameWindow::on_lineEdit_returnPressed() { // some code }
mytextitem.h
// mytextitem.h #ifndef MYTEXTITEM_H #define MYTEXTITEM_H #include <QPainter> #include <QGraphicsTextItem> #include <QObject> // class for customization class MyTextItem : public QGraphicsTextItem { Q_OBJECT public: MyTextItem(QString,int); void StartTimer(); void StopTimer(); void setX_Max(int); void setY_Max(int); void setSpeed(int); int getSpeed(); private: int timerID; int Y_Pos; int Y_Max; int X_Max; int Speed; public slots: void checkResult(); protected: void timerEvent(QTimerEvent *event); }; #endif // MYTEXTITEM_H
mytextitem.cpp
// mytextitem.cpp #include "mytextitem.h" #include "gamewindow.h" #include "ui_gamewindow.h" MyTextItem::MyTextItem(QString txt, int spd) { this->setPlainText(txt); this->setDefaultTextColor(Qt::red); this->setX(100); this->setY(0); setSpeed(spd); Y_Pos = 0; StartTimer(); connect(ui->input_edit,SIGNAL(returnPressed()),this,SLOT(checkResult())); } void MyTextItem::StartTimer() { timerID = startTimer(Speed); } void MyTextItem::StopTimer() { killTimer(timerID); } void MyTextItem::setX_Max(int x) { X_Max = x; } void MyTextItem::setY_Max(int y) { Y_Max = y; } void MyTextItem::setSpeed(int spd) { Speed = spd; } int MyTextItem::getSpeed() { return Speed; } void MyTextItem::checkResult() { // some code } void MyTextItem::timerEvent(QTimerEvent *event) { this->setY(Y_Pos); Y_Pos = Y_Pos + 1; if (Y_Pos > Y_Max){ StopTimer(); } }
Any idea how I can do it the correct way?
Tobias
-
Hi
in void GameWindow::on_pushButton_2_clicked()
after
textitem = new MyTextItem("TEST",80);
you can connectqDebug() << "connect works: " << connect(ui->input_edit,SIGNAL(returnPressed()),textitem ,SLOT(checkResult()));
(must say connect works:true)
and remove connect from
MyTextItem::MyTextItem(QString txt, int spd) -
Thanks!!!! :-)
Now it works!Thank you very very much!!!!!
Tobias
-
@navyseabear
Super :)
When using the SIGNAL()/ SLOT() syntax, its good to always check
it returns true. ( the qDebug() part)
There is actually a new syntax that can fail compile time if something wrong.
https://wiki.qt.io/New_Signal_Slot_Syntax