Search code examples
qtqt4qgraphicssceneqlineeditqpushbutton

Store QLineEdit's data into a QString upon a QPushButton click


I am new to Qt programming. What I want to do is pretty much self explanatory from the title I guess. I have a lineEdit whose data I want to store in a QString when a cretain PushButton is clicked. The problem is the above widgets are inside a graphicsScene so i dont have a on_pushbutton_clicked(); function.

The code for my own solution is below, but as you might have guessed, its not working.

QLineEdit *l = new QLineEdit ;
QPushButton *b = new QPushButton;

QGraphicsProxyWidget *line = scene.addWidget(l);
QGraphicsProxyWidget *button = scene.addWidget(b);

line->setPos(-600,270);
button->setPos(-600,310);
//b->clicked();

QString input;
QString input = l->text(input);


QObject::connect(b,SIGNAL (clicked()),l, SLOT (setText(QString)));

Any kind of help will be greatly appreciated.


Solution

  • I don't see how putting the widgets inside a QGraphicsScene should make a difference here.

    1. Keep a pointer to the QLineEdit "l" in your class, e.g. as QLineEdit* m_lineEdit.

    2. connect b, SIGNAL(clicked()) to a slot buttonClicked().

    3. Implement buttonClicked like this:

      void MyWidget::buttonClicked() {
          const QString text = m_lineEdit->text();
          //do something with "text"
      }