How can I send output to a widget



  • I am building a trivial "desk calculator" to learn ho w to use QT. It has 2 dialog boxes for inputting 2 numbers to be multiplied. It has a QtextEdit called "answerbox". I cannot figure out how to get the reqult into the answerbox. When I compile, I get the error, "answerbox is a non-class type QTextEdit".



  • @DocDJ said in How can I send output to a widget:

    I am building a trivial "desk calculator" to learn ho w to use QT. It has 2 dialog boxes for inputting 2 numbers to be multiplied. It has a QtextEdit called "answerbox". I cannot figure out how to get the reqult into the answerbox. When I compile, I get the error, "answerbox is a non-class type QTextEdit".

    Hi @DocDJ can you share your code ? or at least a sample?


  • Lifetime Qt Champion

    Hi,

    Didn't you forgot to #include <QTextEdit> in the file where you use that class ?



  • @SGaist Thanks for the quick reply. The #include <QTextEdit> is present. Here's my code:
    #include <QtGui>
    #include "djdialog1.h"
    #include <sstream>
    #include <string>
    #include <QTextEdit>

    int int1, int2, answer;
    djdialog1::djdialog1(QWidget *parent)
    : QDialog(parent)
    {
    connect(int1box, SIGNAL(textChanged(const QString &)),
    this, SLOT(on_int1box_textChanged()));
    connect(int2box, SIGNAL(textChanged(const QString &)),
    this, SLOT(on_int2box_textChanged()));
    connect(GoButton, SIGNAL(clicked()), this, SLOT(accept()));
    }
    void djdialog1::on_GoButton_clicked()
    {
    QString qreply;
    qreply=QString::number(answer);
    answerbox.setText(qreply);
    }
    void djdialog1::on_int1box_textChanged(const QString &arg)
    {bool ok;
    int1 = arg.toInt(&ok,10);
    }
    void djdialog1::on_int2box_textChanged(const QString &arg)
    {bool ok;
    int2 = arg.toInt(&ok,10);
    }


  • Moderators

    @DocDJ Can you show the header file, so we can see how answerbox is declared. Also can you please post the whole error message?



  • @DocDJ
    can you provide the header code?



  • @DocDJ

    Hi,

    seems like you created the QTextEdit answerbox in the QT-Designer, Drag & Drop onto your MainWidget?

    That is fine, and really, the normal way to do it. But to access elements of your Designer you have to reference them.
    The default in QT-classes is a UI-Pointer in your case most likly ui, therefore:

    //Change it to this:
    ui->answerbox->setText(qreply);
    

    And you should not get the error any more.



  • @Flotisable
    Here is my header code:

    #ifndef djdialog1_H
    #define djdialog1_H

    #include <QDialog>
    #include "ui_djdialog1.h"

    class djdialog1 : public QDialog, public Ui::djdialog1
    {
    Q_OBJECT
    public:
    djdialog1(QWidget *parent = 0);
    private slots:
    void on_int1box_textChanged(const QString &);
    void on_int2box_textChanged(const QString &);
    void on_GoButton_clicked();
    };
    #endif

    and here is the entire error message:
    djdialog1.cpp:43:: error: request for member 'setText' in '((djdialog1*)this)->
    djdialog1::<anonymous>.Ui::djdialog1::<anonymous>.Ui_djdialog1::answerbox'
    which is of non-class type 'QTextEdit*'

    I also tried: ui->answerbox->setText(qreply) and that said, "ui was not declared in this scope".



  • @DocDJ said in How can I send output to a widget:

    @Flotisable
    Here is my header code:

    #ifndef djdialog1_H
    #define djdialog1_H

    #include <QDialog>
    #include "ui_djdialog1.h"

    class djdialog1 : public QDialog, public Ui::djdialog1
    {
    Q_OBJECT
    public:
    djdialog1(QWidget *parent = 0);
    private slots:
    void on_int1box_textChanged(const QString &);
    void on_int2box_textChanged(const QString &);
    void on_GoButton_clicked();
    };
    #endif

    and here is the entire error message:
    djdialog1.cpp:43:: error: request for member 'setText' in '((djdialog1*)this)->
    djdialog1::<anonymous>.Ui::djdialog1::<anonymous>.Ui_djdialog1::answerbox'
    which is of non-class type 'QTextEdit*'

    I also tried: ui->answerbox->setText(qreply) and that said, "ui was not declared in this scope".

    Your problem is due to a missing namespace Ui, normally this namespace is generated with qtcreator when new designer form class is added, why do you delete them?

    http://doc.qt.io/qt-5/designer-using-a-ui-file.html

    To fix your problem you can add these lines ,

    on your .h

    #ifndef djdialog1_H
    #define djdialog1_H
    
    #include <QDialog>
    #include "ui_djdialog1.h"
    
    
    // add namespace Ui to your .H
    namespace Ui {
        class djdialog1;
    }
    class djdialog1 : public QDialog, public Ui::djdialog1
    {
    Q_OBJECT
    public:
    djdialog1(QWidget *parent = 0);
    private slots:
    void on_int1box_textChanged(const QString &);
    void on_int2box_textChanged(const QString &);
    void on_GoButton_clicked();
    
    private:
        Ui::djdialog1 *ui;//Add uiVar member
    };
    #endif
    

    And your .cpp

    #include <QtGui>
    #include "djdialog1.h"
    #include <sstream>
    #include <string>
    #include <QTextEdit>
    
    int int1, int2, answer;
    djdialog1::djdialog1(QWidget *parent)
        : QDialog(parent),
          ui(new Ui::djdialog1)// initialize ui
    {
    ui->setupUi(this);//setup Ui
    connect(int1box, SIGNAL(textChanged(const QString &)),
    this, SLOT(on_int1box_textChanged()));
    connect(int2box, SIGNAL(textChanged(const QString &)),
    this, SLOT(on_int2box_textChanged()));
    connect(GoButton, SIGNAL(clicked()), this, SLOT(accept()));
    }
    void djdialog1::on_GoButton_clicked()
    {
    QString qreply;
    qreply=QString::number(answer);
    ui->answerbox->setText(qreply);//anserbox from ui
    }
    void djdialog1::on_int1box_textChanged(const QString &arg)
    {bool ok;
    int1 = arg.toInt(&ok,10);
    }
    void djdialog1::on_int2box_textChanged(const QString &arg)
    {bool ok;
    int2 = arg.toInt(&ok,10);
    }
    

    But i advice you to clean this class and add correctly a new designer form class:

    https://www.youtube.com/watch?v=Gpp6dHx4mVw

    Hope this can help ,

    Best regards !



  • @mostefa Thanks for the lines to add. I do not have QtCreator, because a package for it is not available on the Linux platform I am using (ClefOS 6.6) on an older IBM z390 mainframe system that does not have the hardware support for the newer ClefOS system.



  • @DocDJ said in How can I send output to a widget:

    @mostefa Thanks for the lines to add. I do not have QtCreator, because a package for it is not available on the Linux platform I am using (ClefOS 6.6) on an older IBM z390 mainframe system that does not have the hardware support for the newer ClefOS system.

    Ok ,

    So your app work for now?



  • @mostefa Still does not work. NO errors, but no results in answerbox.



  • @DocDJ said in How can I send output to a widget:

    @mostefa Still does not work. NO errors, but no results in answerbox.

    Your signal is not ok, your answer does not contain the result of any operation

    Your signal

    textChanged(const QString &) does not exist for QTextEdit

    Have a look at available signals:

    http://doc.qt.io/qt-4.8/qtextedit.html#signals

    You have to use

    textChanged();

    So i changed your code and multiplication work for me:

    Your header:

    #ifndef djdialog1_H
    #define djdialog1_H
    
    #include <QDialog>
    #include "ui_djdialog1.h"
    
    
    // add namespace Ui to your .H
    namespace Ui {
    class djdialog1;
    }
    class djdialog1 : public QDialog, public Ui::djdialog1
    {
        Q_OBJECT
    public:
        djdialog1(QWidget *parent = 0);
    
    private slots:
        void onInt1boxTextChanged();// slot without arg
        void onInt2boxTextChanged();// slot without arg
    
    you don't need an arg ui->int1box , ui->int2box will be ok
    
        void onGoButtonclicked();//just changed the name of the slot
    
    private:
        Ui::djdialog1 *ui;//Add uiVar member
    };
    #endif
    
    #include <QtGui>
    #include "djdialog1.h"
    #include <sstream>
    #include <string>
    #include <QTextEdit>
    
    int int1, int2, answer;
    djdialog1::djdialog1(QWidget *parent)
        : QDialog(parent),
          ui(new Ui::djdialog1)// initialize ui
    {
        ui->setupUi(this);//setup Ui
        connect(ui->int1box, SIGNAL(textChanged()),
                this, SLOT(onInt1boxTextChanged()));
        connect(ui->int2box, SIGNAL(textChanged()),
                this, SLOT(onInt2boxTextChanged()));
        connect(ui->GoButton, SIGNAL(clicked()), this, SLOT(onGoButtonclicked()));
    }
    void djdialog1::onGoButtonclicked()
    {
        QString qreply;
    
        answer = int1*int2;// your mathematic operation here
        qreply=QString::number(answer);
        ui->answerbox->setText(qreply);//anserbox from ui
    }
    void djdialog1::onInt1boxTextChanged()
    {bool ok;
        int1 = ui->int1box->toPlainText().toInt(&ok,10);
    }
    void djdialog1::onInt2boxTextChanged()
    {bool ok;
        int2 = ui->int2box->toPlainText().toInt(&ok,10);
    }
    

    Hope this can help!



  • @mostefa It still does not produce output. The only difference I can see between your version and mine is that my slot names have the _ character in them (e.g.; on_GoButton_textChanged). Could this make a difference? There are no compiler error or warning messages.


  • Moderators

    @DocDJ Underscores doesn't matter.
    You should really check whether all connect calls succeed.
    Can you post your header and cpp file?



  • @DocDJ said in How can I send output to a widget:

    @mostefa It still does not produce output. The only difference I can see between your version and mine is that my slot names have the _ character in them (e.g.; on_GoButton_textChanged). Could this make a difference? There are no compiler error or warning messages.

    I tested the code beofre i posted here and it work, looks like there is something you're doing wrong

    You said :
    There are no compiler error or warning messages.

    Ok

    But is there any output (errors/informations) when your app is running?



  • Sorry I couldn't reply sooner. There are no error messages (compiling or running) , no output of any kind and my GUI window just sits there.



  • @jsulm I have modified my code to be exactly like that posted by @mostefa



  • @DocDJ said in How can I send output to a widget:

    @jsulm I have modified my code to be exactly like that posted by @mostefa

    Could you share with us the content of your .ui file?



  • @mostefa Here is my .ui file:
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>djdialog1</class>
    <widget class="QDialog" name="djdialog1">
    <property name="windowModality">
    <enum>Qt::ApplicationModal</enum>
    </property>
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>334</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>DJs-Dialog</string>
    </property>
    <widget class="QTextEdit" name="int1box">
    <property name="geometry">
    <rect>
    <x>73</x>
    <y>10</y>
    <width>61</width>
    <height>41</height>
    </rect>
    </property>
    <property name="toolTip">
    <string>int 1 here</string>
    </property>
    </widget>
    <widget class="QTextEdit" name="int2box">
    <property name="geometry">
    <rect>
    <x>73</x>
    <y>70</y>
    <width>61</width>
    <height>41</height>
    </rect>
    </property>
    </widget>
    <widget class="QLabel" name="label">
    <property name="geometry">
    <rect>
    <x>160</x>
    <y>10</y>
    <width>60</width>
    <height>31</height>
    </rect>
    </property>
    <property name="text">
    <string>first int</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignCenter</set>
    </property>
    </widget>
    <widget class="QLabel" name="label_2">
    <property name="geometry">
    <rect>
    <x>160</x>
    <y>70</y>
    <width>91</width>
    <height>21</height>
    </rect>
    </property>
    <property name="text">
    <string>second int</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignCenter</set>
    </property>
    </widget>
    <widget class="QTextEdit" name="answerbox">
    <property name="geometry">
    <rect>
    <x>70</x>
    <y>150</y>
    <width>104</width>
    <height>41</height>
    </rect>
    </property>
    </widget>
    <widget class="QLabel" name="label_3">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>80</y>
    <width>60</width>
    <height>31</height>
    </rect>
    </property>
    <property name="styleSheet">
    <string notr="true">color: rgb(0, 0, 255);
    font: 16pt "Bitstream Charter";</string>
    </property>
    <property name="text">
    <string>+</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignCenter</set>
    </property>
    </widget>
    <widget class="QPushButton" name="GoButton">
    <property name="geometry">
    <rect>
    <x>90</x>
    <y>230</y>
    <width>87</width>
    <height>27</height>
    </rect>
    </property>
    <property name="text">
    <string>Compute</string>
    </property>
    </widget>
    <widget class="QPushButton" name="clearbutton">
    <property name="geometry">
    <rect>
    <x>200</x>
    <y>230</y>
    <width>87</width>
    <height>27</height>
    </rect>
    </property>
    <property name="text">
    <string>Clear data</string>
    </property>
    </widget>
    </widget>
    <resources/>
    <connections/>
    </ui>



  • @DocDJ said in How can I send output to a widget:

    @mostefa Here is my .ui file:
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>djdialog1</class>
    <widget class="QDialog" name="djdialog1">
    <property name="windowModality">
    <enum>Qt::ApplicationModal</enum>
    </property>
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>334</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>DJs-Dialog</string>
    </property>
    <widget class="QTextEdit" name="int1box">
    <property name="geometry">
    <rect>
    <x>73</x>
    <y>10</y>
    <width>61</width>
    <height>41</height>
    </rect>
    </property>
    <property name="toolTip">
    <string>int 1 here</string>
    </property>
    </widget>
    <widget class="QTextEdit" name="int2box">
    <property name="geometry">
    <rect>
    <x>73</x>
    <y>70</y>
    <width>61</width>
    <height>41</height>
    </rect>
    </property>
    </widget>
    <widget class="QLabel" name="label">
    <property name="geometry">
    <rect>
    <x>160</x>
    <y>10</y>
    <width>60</width>
    <height>31</height>
    </rect>
    </property>
    <property name="text">
    <string>first int</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignCenter</set>
    </property>
    </widget>
    <widget class="QLabel" name="label_2">
    <property name="geometry">
    <rect>
    <x>160</x>
    <y>70</y>
    <width>91</width>
    <height>21</height>
    </rect>
    </property>
    <property name="text">
    <string>second int</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignCenter</set>
    </property>
    </widget>
    <widget class="QTextEdit" name="answerbox">
    <property name="geometry">
    <rect>
    <x>70</x>
    <y>150</y>
    <width>104</width>
    <height>41</height>
    </rect>
    </property>
    </widget>
    <widget class="QLabel" name="label_3">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>80</y>
    <width>60</width>
    <height>31</height>
    </rect>
    </property>
    <property name="styleSheet">
    <string notr="true">color: rgb(0, 0, 255);
    font: 16pt "Bitstream Charter";</string>
    </property>
    <property name="text">
    <string>+</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignCenter</set>
    </property>
    </widget>
    <widget class="QPushButton" name="GoButton">
    <property name="geometry">
    <rect>
    <x>90</x>
    <y>230</y>
    <width>87</width>
    <height>27</height>
    </rect>
    </property>
    <property name="text">
    <string>Compute</string>
    </property>
    </widget>
    <widget class="QPushButton" name="clearbutton">
    <property name="geometry">
    <rect>
    <x>200</x>
    <y>230</y>
    <width>87</width>
    <height>27</height>
    </rect>
    </property>
    <property name="text">
    <string>Clear data</string>
    </property>
    </widget>
    </widget>
    <resources/>
    <connections/>
    </ui>

    Your ui file is ok too

    I think i found the problem, maybe you don't have an instance of djdialog on your main?

    What do you have on your main.cpp file ?

    replace your main.cpp with the following code and everything will be ok (i hope)

    #include <QApplication>
    
    #include "djdialog1.h"
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        djdialog1 w;// create an instance of your form 
        w.show();// show your form 
    
        return a.exec();
    }
    

    Keep me informed !



  • @mostefa THANK YOU, THANK YOU!!! That did it. I will analyze it to understand what I did wrong.



  • @DocDJ said in How can I send output to a widget:

    @mostefa THANK YOU, THANK YOU!!! That did it. I will analyze it to understand what I did wrong.

    So the problem is solved now?

    Good !

    I advice you to read more about qt , if you want to go further

    http://doc.qt.io/qt-5/gettingstarted.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.