Calling text() after the connect



  • i post it 2nd time, coz everybody try to help me and just indicate a problem, but i cant solve it by myself =(
    main only:
    @

    QApplication a(argc, argv);
    QWidget w;
    QComboBox* cmb = new QComboBox;
    QPushButton* btn = new QPushButton("add");
    QDial* dial = new QDial;
    QLabel* lbl = new QLabel(" ");

    cmb->addItem("item1");
    cmb->addItem("dial");
    cmb->addItem("item3");
    

    dial->hide();

    if (lbl->text() == "dial") {
    QObject::connect(btn, SIGNAL(clicked()),
    dial, SLOT (show())
    );
    }
    QObject::connect(cmb, SIGNAL(currentTextChanged(QString)),
    lbl, SLOT (setText(QString))
    );
    @
    i can change lbl for "item1", "dial", "item3", but lbl->text is always item1.
    calling text() after the connect will of course give you the text of the first item, because it is executed before you change the selection in the GUI.
    ok. but how can i solve it. just write specific lines. ty



  • Hi,

    simply you can't do it without writing a cusom SLOT.
    You musy write your QWidget subclass and connect the currentTextChanged signal to a custom slot like this

    @
    void MyWidget::mySlot (QString _text)
    {
    lbl->setText(_text);
    if (_text == "dial")
    QObject::connect(btn, SIGNAL(clicked()), dial, SLOT (show()));

    }
    @



  • i have no expiriance in it...
    correct it plz:
    .h
    @

    class MyWidget : public QObject
    {
    Q_OBJECT
    private:
    QString _text;
    public:
    MyWidget();
    public slots:
    void MySlot(QString _text);

    };
    @

    .cpp:
    @
    MyWidget::MyWidget() :
    QObject()
    ,_text

    {

    }

    void MyWidget::mySlot(QString _text)
    {
    lbl->setText(_text);
    if (_text == "dial")
    QObject::connect(btn, SIGNAL(clicked()),
    dial, SLOT (show()));

    }
    @

    main is ok now



  • Hi,

    I'd like to help you but I think you MUST study a little bit more about Qt before to ask some kind of help.
    What you're asking isn't to help you but to write code for you. If we do this, you don't learn anything.

    I hope you understand why I do this.



  • ok i will
    ty for tolerance
    i try to do my best



  • but its dificult to learn it w/o communication.
    i have lot of questions. there are no answers in books((



  • Hi,

    obviously you can ask, I only suggested to study a litte bit before to post questions.

    Sometime is faster read a manual/documentatios than post a question and wait for an aswer



  • mcosta,
    may be its willfully, boldly and foolishly a little, but i still need answer ( i rly need this, * just this* lines) in my .h and .cpp files
    can u allocate 10 mins for me and i will back to my book
    (sry for my english too)



  • Hi,

    this is a code example

    MyWidget.h
    @
    #ifndef MYWIDGET_H
    #define MYWIDGET_H

    #include <QWidget>

    class MyWidget: public QWidget
    {
    Q_OBJECT

    public:
    MyWidget (QWidget* _parent = 0);

    private slots:
    void mySlot (QString _text);

    private:
    QComboBox *cmb;
    .....
    };
    #endif // MYWIDGET_H
    @

    MyWidget.cpp
    @
    #include "MyWidget.h"

    MyWidget::MyWidget (QWidget* _parent): QWidget (_parent)
    {
    cmb = new QComboBox (this);
    ...

    connect(cmb, SIGNAL(currentTextChanged(QString)),
    this, SLOT (mySlot(QString))
    }

    void MyWidget::mySlot(QString _text)
    {
    lbl->setText (_text);
    if (_text == "dial")
    connect(btn, SIGNAL(clicked()), dial, SLOT (show()));
    }
    @



  • Thank you for everything. I appreciate this


Log in to reply
 

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