-2

I have a GUI built using the QT Creator. At some point a Dialog window is opened to which I need to send a variable of type QStringList. I do this using the signals and slots method. However, the variable is empty once sent. Here is some code samples:

widget.h

class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
signals:
void mySignal(QStringList);
};

widget.cpp

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
 // blah blah
}

Widget::~Widget()
{
    delete ui;
}

  void Widget::on_pushButton_4_clicked()
{
    QStringList dList;
    int damount = ui->listWidget->count();
    for(int i=0; i < damount; i++){
                                    dList << ui->listWidget->item(i)->text();
                                    qDebug() << dList;
                                    }
    emit mySignal(dList);

    mysaver mDialog;
    mDialog.setModal(true);
    mDialog.exec();
}

mysaver.h (The dialog box)

class mysaver : public QDialog
{
Q_OBJECT
public:
explicit mysaver(QWidget *parent = 0);
~mysaver();

public slots:
void myreciever(QStringList);
}

mysaver.cpp

void mysaver::myreciever(QStringList aList)
{
qDebug << aList;
}

main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
Widget w;

Widget *duff = new Widget;
mysaver *buff = new mysaver;
QObject::connect(duff,SIGNAL(mySignal(QStringList)),buff,SLOT(myreciever(QStringList)));

w.show();

return a.exec();
}

I'd really appreciate some help on this. Note: If I'm doing this whole method wrong and should be doing something entirely different then TELL ME!

Zynky
  • 1
  • 4
  • QObject::connect(duff,SIGNAL(mySignal(QVariant)),buff,SLOT(myreciever(QVariant))); - why are you using QVariant and not QStringList? – demonplus Aug 19 '15 at 11:59
  • @demonplus Sorry it was a typo. – Zynky Aug 19 '15 at 12:21
  • I can't see anything obviously wrong with this code, is `damount` greater than zero? – cmannett85 Aug 19 '15 at 12:21
  • Your code is absolutely wrong and it seems you are lacking of understanding of signals and slots. Please, reread the documentation or some Qt-book. You use this totally wrong. And this can't be explained just in one answer. – VP. Aug 19 '15 at 12:31
  • @cmannett85 "damount" is always the value I expect it to be but thanks for the suggestion. – Zynky Aug 19 '15 at 12:32
  • As noted by victor, you are connecting to a `mysaver` object you create in `main` (you never show this dialog), but in `Widget::on_pushButton_4_clicked()` you create a new `mysaver` object to which you never connect the signal. I don't know if this is lack of OOP knowledge or just a mistake. – thuga Aug 19 '15 at 12:36
  • @thuga27 Okay, I'll try using your suggestion. It probably is lack of knowledge given I'm very new to Qt. – Zynky Aug 19 '15 at 12:38

2 Answers2

1

You are creating two mysaver instances and only connecting to the first (invisible) one:

// In main.cpp
mysaver *buff = new mysaver;

// In Widget::on_pushButton_4_clicked()
mysaver mDialog;
mDialog.setModal(true);
mDialog.exec();

mDialog is not the mysaver instance you connected to.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • This is the correct solution and everything works as I need it to. Thanks for your patience. – Zynky Aug 19 '15 at 12:49
0

Obviously you are using QVariant instead of QStringList and there is no compilation-time check. It is better to make connect in this way:

QObject::connect(duff, &Widget::mySignal, buff, &mysaver::myreciever);

You will have check of types in compilation time.

demonplus
  • 5,613
  • 12
  • 49
  • 68
  • However, sometimes the new syntax is much more difficult than old `SIGNAL` and `SLOT`, unfortunately.. – VP. Aug 19 '15 at 12:05
  • Thanks for your suggestion. The issue regarding QVariant was a typo for which I'm sorry. I've tried making the connection using your syntax but the QStringList "aList" is still empty. – Zynky Aug 19 '15 at 12:11