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. Reading text from textEdit
Forum Updated to NodeBB v4.3 + New Features

Reading text from textEdit

Scheduled Pinned Locked Moved Solved General and Desktop
textedit
7 Posts 3 Posters 5.5k Views 2 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.
  • G Offline
    G Offline
    gabor53
    wrote on 5 Mar 2016, 04:39 last edited by
    #1

    Hi,
    I'd like to read the text from this textEdit and store it in a QString:

    QTextEdit *descr_TextEdit = new QTextEdit;
    
            descr_TextEdit->setFixedWidth (400);
            descr_TextEdit->setFixedHeight (50);
           description = descr_TextEdit->toPlainText ();
            
            qDebug() << "Description: " << description;
    

    This doesn't generate any error messages, but the QString description is empty. Please help me to find a better solution. Thank you.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 5 Mar 2016, 05:37 last edited by
      #2

      Between the first line and last line of your code there is no opportunity for a user to manipulate the text in the QTextEdit. Since you have not set that text it is the default empty editor.

      At the very least the QTextEdit needs to be shown to the user directly (QWidget::show()) or indirectly through inclusion in a larger UI. The first three lines would usually be in the constructor of the class representing a UI, and the other lines would be elsewhere in the program, often a slot.

      G 1 Reply Last reply 5 Mar 2016, 14:54
      1
      • C ChrisW67
        5 Mar 2016, 05:37

        Between the first line and last line of your code there is no opportunity for a user to manipulate the text in the QTextEdit. Since you have not set that text it is the default empty editor.

        At the very least the QTextEdit needs to be shown to the user directly (QWidget::show()) or indirectly through inclusion in a larger UI. The first three lines would usually be in the constructor of the class representing a UI, and the other lines would be elsewhere in the program, often a slot.

        G Offline
        G Offline
        gabor53
        wrote on 5 Mar 2016, 14:54 last edited by
        #3

        @ChrisW67
        I did what you suggested:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
        	QTextEdit *textEdit = new QTextEdit(thi);
        	textEdit->setFixedWidth (400);
            textEdit->setFixedHeight (50);
        
            QString description;
            description = textEdit->toPlainText ();
        
         	textEdit->show ();
        
        	qDebug() << "Description: " << description;
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        

        I still have the same problem: still don't know how to capture the text in textedit and transfer it to description. Thank you for your help.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          OwonaVivienD
          wrote on 5 Mar 2016, 15:05 last edited by
          #4

          Hi, ChrisW67

          That's pretty much normal. You haven't written anything in your QTextEdit between its creation and the moment at which you call QTextEdit::toPlainText; that's why the QString instance "description" is empty. You should wait that the QTextEdit gets filled with some text before calling QTextEdit::toPlainText and showing this, for instance with qDebug().

          I hope that it will help you.

          Have a nice day.

          By the way, "Vivien" is a male name in French...

          G 1 Reply Last reply 5 Mar 2016, 15:19
          0
          • O OwonaVivienD
            5 Mar 2016, 15:05

            Hi, ChrisW67

            That's pretty much normal. You haven't written anything in your QTextEdit between its creation and the moment at which you call QTextEdit::toPlainText; that's why the QString instance "description" is empty. You should wait that the QTextEdit gets filled with some text before calling QTextEdit::toPlainText and showing this, for instance with qDebug().

            I hope that it will help you.

            Have a nice day.

            G Offline
            G Offline
            gabor53
            wrote on 5 Mar 2016, 15:19 last edited by
            #5

            @OwonaVivienD
            How can I "wait"?

            1 Reply Last reply
            0
            • O Offline
              O Offline
              OwonaVivienD
              wrote on 5 Mar 2016, 15:36 last edited by
              #6

              You should write a separate method/slot that performs the whole text "capture'n'show" and that will be triggered by an element of you choice. For example, this element can be a QTimer and its signal QTimer::timeout or a QPushButton and its signal QPushButton::pressed; you just have to do a brainstorming with your imagination. ;)

              By the way, "Vivien" is a male name in French...

              G 1 Reply Last reply 5 Mar 2016, 15:47
              1
              • O OwonaVivienD
                5 Mar 2016, 15:36

                You should write a separate method/slot that performs the whole text "capture'n'show" and that will be triggered by an element of you choice. For example, this element can be a QTimer and its signal QTimer::timeout or a QPushButton and its signal QPushButton::pressed; you just have to do a brainstorming with your imagination. ;)

                G Offline
                G Offline
                gabor53
                wrote on 5 Mar 2016, 15:47 last edited by
                #7

                @OwonaVivienD
                Thank you. I think I will go with a QPushButton.

                1 Reply Last reply
                0

                7/7

                5 Mar 2016, 15:47

                • Login

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