Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect QLineEdit with my class

Connect QLineEdit with my class

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 6 Posters 5.2k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    navyseabear
    wrote on last edited by
    #9

    Hmm, and how do I do that?
    I'm really new in C++ and Qt. Normaly I use Pascal and VB.

    Tobias

    jsulmJ 1 Reply Last reply
    0
    • N navyseabear

      Hmm, and how do I do that?
      I'm really new in C++ and Qt. Normaly I use Pascal and VB.

      Tobias

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #10

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        navyseabear
        wrote on last edited by
        #11

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #12

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            navyseabear
            wrote on last edited by
            #13

            Yes, and I do not know how to connect them to the QLineEdit.

            Tobias

            mrjjM 1 Reply Last reply
            0
            • N navyseabear

              Yes, and I do not know how to connect them to the QLineEdit.

              Tobias

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #14

              @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.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                navyseabear
                wrote on last edited by
                #15

                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

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #16

                  Hi
                  in void GameWindow::on_pushButton_2_clicked()
                  after
                  textitem = new MyTextItem("TEST",80);
                  you can connect

                  qDebug() << "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)

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    navyseabear
                    wrote on last edited by
                    #17

                    Thanks!!!! :-)
                    Now it works!

                    Thank you very very much!!!!!

                    Tobias

                    mrjjM 1 Reply Last reply
                    1
                    • N navyseabear

                      Thanks!!!! :-)
                      Now it works!

                      Thank you very very much!!!!!

                      Tobias

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #18

                      @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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved