Results 1 to 6 of 6

Thread: Connecting textEdit with label

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Connecting textEdit with label

    I tried to connect a textEdit to a label on a different page using stackedWidget, so that when a text is written in textEdit the same text is shown in the label on the next page, but so far it didnt work out. My textEdits Name is "textEdit_name" and my labels Name is "label_name". My Code so far is

    Qt Code:
    1. void Volk_Auswahl::label_name()
    2. {
    3. connect(ui->textEdit_name, SIGNAL(textChanged()), this, SLOT(label_name()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    There is no error but when I type something in textEdit there is no text in label.
    Last edited by ejoty; 25th June 2020 at 12:22.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting textEdit with label

    And what is doing slot label_name() ?

  3. #3
    Join Date
    Jun 2020
    Posts
    11
    Qt products
    Qt4 Qt5

    Default Re: Connecting textEdit with label

    I dont know which funtion to use. Sry I am new at this.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Connecting textEdit with label

    Showing 4 lines of code is not going to help us understand how to help you with your problem.

    If this is basically the same problem as your other post, then please don't post the same question twice.

    So I assume you have two QWidget-based classes that are the two widgets you have created for your two stack widget pages. One of these has a QTextEdit (or a QLineEdit - which you should be using if all you want is for the user to be able to enter a short string, like a name) and the other widget has a QLabel. You want the QLabel to display the same text that the user types into the QTextEdit or QLineEdit, right?

    The code you posted above makes no sense at all, or maybe you don't really understand signals and slots. The connect() statement is basically saying, "When the first widget emits this signal, I want you to send it to this slot of the second widget". It prepares the two widgets for something that will happen at some time in the future. It is not an instruction to do something now. So putting a connect() statement inside a slot doesn't cause anything to happen (except add another connection each time that slot is called - probably not what you want).

    The correct procedure, and one that creates an abstraction so that the widget on stack position 0 doesn't have to know anything about the contents of the widget on stack position 1 is to implement a signal and slot pair that can relay the signal from the QTextEdit / QLineEdit to the QLabel, through their parent widgets.

    Something like this. Don't copy and paste this code. Study it and understand what it is doing.

    Qt Code:
    1. // Widget0 is the QWidget that contains the QTextEdit
    2. class Widget0 : public QWidget
    3. {
    4. Q_OBJECT;
    5.  
    6. public:
    7. Widget0( QWidget * parent = nullptr );
    8.  
    9. // ...
    10. signals:
    11. void nameChanged( const QString & name );
    12.  
    13. private slots:
    14. void onTextChanged();
    15.  
    16. private:
    17. Widget0::Ui ui; // Contains the QTextEdit or QLineEdit
    18. };
    19.  
    20. // In the constructor, set up a signal slot connection so the QTextEdit can tell Widget0
    21. // when its contents have changed
    22. Widget0::Widget0( QWidget * parent )
    23. : QWidget( parent )
    24. {
    25. ui.setupUi( this );
    26.  
    27. connect( ui.textEdit, SIGNAL( textChanged() ), this, SLOT( onTextChanged() ) );
    28. }
    29.  
    30. // Slot implementation. When the text has changed, this will be called.
    31. // In it, we get the contents of the QTextEdit as an ordinary string,
    32. // then emit our signal so the rest of the world can be notified
    33. void Widget0::onTextChanged()
    34. {
    35. QString text = ui->textEdit->toPlainText();
    36. emit nameChanged( text );
    37. }
    38.  
    39. // Widget1 is the QWidget that contains the QLabel
    40. class Widget1 : public QWidget
    41. {
    42. Q_OBJECT;
    43.  
    44. public:
    45. Widget1( QWidget * parent = nullptr );
    46. // ...
    47.  
    48. public slots:
    49. void onNameChanged( const QString & name );
    50.  
    51. private:
    52. Widget1::Ui ui; // contains the QLabel
    53. };
    54.  
    55. Widget1::Widget1( QWidget * parent )
    56. : QWidget( parent )
    57. {
    58. ui.setupUi( this );
    59. }
    60.  
    61. // Slot. Sets the label text
    62. void Widget1::onNameChanged( const QString & name )
    63. {
    64. ui.label_name->setText( name );
    65. }
    66.  
    67.  
    68. // In your MainWindow constructor is where you connect Widget0 and Widget1
    69. // together
    70.  
    71. MainWindow::MainWindow( QWidget * parent )
    72. : QMainWindow( parent )
    73. {
    74. ui.setupUi( this );
    75.  
    76. // The magic part. Now the QTextEdit's signal gets relayed through Widget0
    77. // to the slot in Widget1, which sets the label text.
    78. connect( ui.widget0, SIGNAL( nameChanged( const QString & ) ), ui.widget1, SLOT( onNameChanged( const QString & ) ) );
    79. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 3rd November 2016, 23:27
  2. Qml TextEdit
    By goli in forum Newbie
    Replies: 4
    Last Post: 28th April 2011, 14:37
  3. Inserting label inside textEdit widget
    By dshan in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2011, 11:17
  4. Replies: 3
    Last Post: 12th May 2010, 23:53
  5. How to make text in label or TextEDit to flash?
    By qtlinuxnewbie in forum Newbie
    Replies: 2
    Last Post: 6th April 2010, 07:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.