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,
this is a code example
MyWidget.h
@
#ifndef MYWIDGET_H
#define MYWIDGET_H#include <QWidget>
class MyWidget: public QWidget
{
Q_OBJECTpublic:
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()));
}
@