You are not logged in.

#1 2009-04-14 04:08:58

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] Simple QT Question: Connecting Widgets to Slots

I'm trying to make a very simple QT application with an LCD widget that starts out at 0, but when the user clicks the button below it, the LCD changes to read "5." The code I have right now compiles just fine but when I click the button, nothing seems to happen...

#include <QtGui>

int main(int argc, char *argv[]) {

    QApplication app(argc, argv);

    QWidget *window = new QWidget;
    QLCDNumber *item1 = new QLCDNumber(2);
    QPushButton *item2 = new QPushButton("Make LCD read '5.'");

    QObject::connect(item2, SIGNAL(clicked()), item1, SLOT(display(5)));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(item1);
    layout->addWidget(item2);

    window->setLayout(layout);
    window->show();

    return app.exec();

}

Can anyone help me? Thanks in advance!

Last edited by tony5429 (2009-06-20 20:35:09)

Offline

#2 2009-04-14 10:35:27

Adams
Member
From: Poland
Registered: 2007-02-12
Posts: 14

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

There is no such slot - 'display(5)'. (But there is 'display(int)')Parameters to slots are automatically passed from signals. As there is no 'clicked(int)', it would be best for you to create a new class derived from QLCDNumber and create a new slot in it. Something like the code below:

/*
 * file: MyLCD.h
 */
#include <QtGui>

class MyLCD : public QLCDNumber{
    Q_OBJECT

    public:
        MyLCD();
    public slots:
        void display();
}
;

/*
 * file: MyLCD.cxx
 */
#include "MyLCD.h"

void MyLCD::display(){
    QLCDNumber::display(5);
}

MyLCD::MyLCD():QLCDNumber(2){
}

/*
 * file: main.cxx
 */
#include <QtGui>
#include "MyLCD.h"

int main(int argc, char *argv[]) {

    QApplication app(argc, argv);

    QWidget *window = new QWidget;
    MyLCD *item1 = new MyLCD();
    QPushButton *item2 = new QPushButton("Make LCD read '5.'");
    QObject::connect(item2, SIGNAL(clicked()), item1, SLOT(display()));
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(item1);
    layout->addWidget(item2);

    window->setLayout(layout);
    window->show();

    return app.exec();

}

"The bureaucracy is expanding to meet the needs of an expanding bureaucracy"

Offline

#3 2009-04-14 12:26:59

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

The other option is a QSignalMapper:
(Not sure if the other #include is needed, I would assume it's not since QSignalMapper is in core)

#include <QtGui>
#include <QSignalMapper>

int main(int argc, char *argv[]) {

        QApplication app(argc, argv);

        QWidget *window = new QWidget;
        QSignalMapper *signalMapper = new QSignalMapper(window );
        QLCDNumber *item1 = new QLCDNumber(2);
        QPushButton *item2 = new QPushButton("Make LCD read '5.'");

        QObject::connect(item2, SIGNAL(clicked()), signalMapper, SLOT(map()));
        signalMapper->setMapping(item2, 5);
        QObject::connect(signalMapper, SIGNAL(mapped(int)), item1, SLOT(display(int)));

        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(item1);
        layout->addWidget(item2);

        window->setLayout(layout);
        window->show();

        return app.exec();
}

Offline

#4 2009-04-14 13:12:05

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Thanks, scio! It worked! And no, I didn't need to include <QSignalMapper>.

Offline

#5 2009-04-15 06:08:22

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Well, another question now. If I want to reset a combo box using signal mapping, does anyone have any idea why this won't work? (It compiles but clicking the reset button doesn't do anything).

#include <QtGui>

int main(int argc, char *argv[]) {

    QApplication app(argc, argv);

    QWidget *window = new QWidget;
    QSignalMapper *signalMapper = new QSignalMapper(window);
    QComboBox *item1 = new QComboBox();
    item1->insertItem(0, "zero");
    item1->insertItem(1, "one");
    item1->insertItem(2, "two");
    item1->insertItem(3, "three");
    QPushButton *item2 = new QPushButton("Reset combo box.");

    QObject::connect(item2, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(item2, 0);
    QObject::connect(signalMapper, SIGNAL(mapped(int)), item1, SLOT(setCurrentItem(int)));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(item1);
    layout->addWidget(item2);

    window->setLayout(layout);
    window->show();

    return app.exec();

}

Offline

#6 2009-04-20 08:09:38

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Any ideas, anyone?

Offline

#7 2009-04-20 16:04:37

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Did you try running your code in a terminal?  I got this error:

Object::connect: No such slot QComboBox::setCurrentItem(int)

Since QComboBox has setCurrentIndex not setCurrentItem.

Hope that helps.

Offline

#8 2009-04-21 02:17:03

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Thanks! That fixed it. Silly me; I had just misread the qcombobox page on qt's website.

Is there an easy way to invoke a generic function with QT? I have a function in the c++ code called "sendemail()" - can I do something like...

QObject::connect(item2, SIGNAL(clicked()), this_cpp_code, SLOT(sendemail());

Offline

#9 2009-04-21 12:21:24

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

If you declare the function to be a slot you can:

#include <QObject>

class EmailComposer : public QObject
{
    Q_OBJECT

public:
    EmailComposer() {}

public slots:
    void sendemail();
};

instead of:

#include <QObject>

class EmailComposer : public QObject
{
    Q_OBJECT

public:
    EmailComposer() {}
    void sendemail();
};

Edit: I think I just realized that you meant the function is not in a class, right?  If that is the case, I don't know of a way to make that work.  Qt is heavily object-oriented and I think you can only call slots and signals on objects.

Last edited by scio (2009-04-21 12:26:44)

Offline

#10 2009-04-30 00:02:39

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Thanks! Actually, I figured it out. I needed item2 to call the function when clicked, and then an output from the function to be displaed in item1 (a QLabel). So I set it up like this...

QObject::connect(item2, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(item2, sendemail());
QObject::connect(signalMapper, SIGNAL(mapped(QString)), item1, SLOT(setText(QString)));

It works great! One issue though: sendemail() should actually be sendemail(char *, int, char *) which pulls in a string from a QLineEdit, an index from a QComboBox, and a string from a QTextEdit. Does anyone know how I can pull out these values for sendemail()?

Offline

#11 2009-04-30 03:52:35

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Here is what I'm really asking. Is there a way to do this?

QObject::connect(item1, SIGNAL(clicked()), item2, SLOT(sprintf(str, text())));

When item1 is clicked, I want to store the text of item2 into the variable "str." Does anyone know how I can do that? Thanks in advance...

Offline

#12 2009-05-01 14:07:14

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

You will have to write a slot function on a class to do that one I think. i.e:

#include <QObject>
#include <QString>

class EmailComposer : public QObject
{
    Q_OBJECT

public:
    EmailComposer() {}

public slots:
    void sendemail(char *, int, char *);
    void setStoredText(const QString &text) {
       storedText = text;
    }
private:
   QString storedText;
};

Or something similar if I understand you correctly.

Edit: Cleanup

Last edited by scio (2009-05-01 14:07:54)

Offline

#13 2009-05-18 22:51:10

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

scio: can that go in the main.cpp file? And if so, does it go before, after, or inside the main function? Thanks!

Offline

#14 2009-05-18 23:31:51

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Um, if you are trying to keep it all in 1 file, it would go before the main function.  Ideally you would have 1 file for each class and then the main file.

Offline

#15 2009-05-19 00:25:35

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Ah; okay. How do I reference the public slot after I've created it in the class? i.e., I'm trying this and it doesn't seem to be working...

QObject::connect(item1, SIGNAL(clicked()), EmailComposer, SLOT(sendemail("hi", 5, "bye")));

Offline

#16 2009-05-19 13:04:03

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

tony5429 wrote:

Ah; okay. How do I reference the public slot after I've created it in the class? i.e., I'm trying this and it doesn't seem to be working...

QObject::connect(item1, SIGNAL(clicked()), EmailComposer, SLOT(sendemail("hi", 5, "bye")));

replace EmailComposer with the instance of the class like you did in the original code and be sure to use a signal mapper, or make the slot have the same, or less arguments:

QObject::connect(item1, SIGNAL(clicked()), composerInstance, SLOT(sendemail()));

Edit: I speak English good...

Last edited by scio (2009-05-19 13:04:35)

Offline

#17 2009-05-19 17:36:22

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Thanks so much for your help. I apologise as I am having a lot of trouble getting this simple code to work. I will try to explain my problem fully... Here is my main.cpp, the only file I am using:

#include <QtGui>

const char * OPS[] = {
    "hi",
    "1",
    "2",
    "3"
};

class EmailComposer : public QObject {
    Q_OBJECT

    public:
        EmailComposer() {}

    public slots:
        void makenewfile() {
            system("echo \"it worked!\" > newfile.txt");
        }

    private:
        QString storedText;
};

int main(int argc, char *argv[]) {

    //define qapplication class
    QApplication app(argc, argv);

    //this line causes the error...
    EmailComposer *composerInstance = new EmailComposer;

    //declare variables
    int i;

    //create qt widgets and signal mapper
    QWidget *window = new QWidget;
    QSignalMapper *signalMapper = new QSignalMapper(window);
    QLabel *item1 = new QLabel("Status: ready...");
    QLabel *item2 = new QLabel("Number:");
    QLineEdit *item3 = new QLineEdit();
    QLabel *item4 = new QLabel("Options:");
    QComboBox *item5 = new QComboBox();
    item5->insertItem(0, "0");
    for ( i = 1 ; i <= (signed)(sizeof(OPS)/sizeof(OPS[0])) ; i++ )
        item5->insertItem(i, OPS[i-1]);
    QLabel *item6 = new QLabel("begin!");
    QTextEdit *item7 = new QTextEdit();
    QPushButton *item8 = new QPushButton("Send");

    //make item1 say "clicked!" when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(item8, "clicked!");
    QObject::connect(signalMapper, SIGNAL(mapped(QString)), item1, SLOT(setText(QString)));

    //run makenewfile() when item8 is clicked (this doesn't work because  I can't create the EmailComposer)
    QObject::connect(item8, SIGNAL(clicked()), composerInstance, SLOT(makenewfile()));

    //clear item3 when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), item3, SLOT(clear()));

    //reset item5 when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(item8, 0);
    QObject::connect(signalMapper, SIGNAL(mapped(int)), item5, SLOT(setCurrentIndex(int)));

    //clear item7 when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), item7, SLOT(clear()));

    //create layout and add widgets to layout
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(item1, 0, 0, 1, 2);
    layout->addWidget(item2, 2, 0);
    layout->addWidget(item3, 2, 1);
    layout->addWidget(item4, 3, 0);
    layout->addWidget(item5, 3, 1);
    layout->addWidget(item6, 4, 0, 1, 2);
    layout->addWidget(item7, 5, 0, 1, 2);
    layout->addWidget(item8, 6, 0, 1, 2);

    //output to screen
    window->setLayout(layout);
    window->show();

    //pass control to qt
    return app.exec();
}

And here is the error I get when I try to compile it...

[karam@nikolai qt_proj]$ qmake -project
[karam@nikolai qt_proj]$ qmake
[karam@nikolai qt_proj]$ make
g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -I. -o main.o main.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/usr/lib -o qt_proj main.o    -L/usr/lib -lQtGui -L/usr/lib -L/usr/X11R6/lib64 -pthread -lpng -lfreetype -lgobject-2.0 -lSM -lICE -pthread -pthread -lXrender -lfontconfig -lXext -lX11 -lQtCore -lz -lm -pthread -lgthread-2.0 -lrt -lglib-2.0 -ldl -lpthread
main.o: In function `main':
main.cpp:(.text+0x4d): undefined reference to `vtable for EmailComposer'
collect2: ld returned 1 exit status
make: *** [qt_proj] Error 1
[karam@nikolai qt_proj]$

As noted in the main.cpp code, the error only occurs if I use the line "EmailComposer *composerInstance = new EmailComposer;"

Do you have any ideas what is going on? Thanks again!

Last edited by tony5429 (2009-05-20 04:24:31)

Offline

#18 2009-05-20 13:16:49

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Easiest solution is to use separate files for the class and main function:
main.cpp:

#include <QtGui>
#include "emailcomposer.h"

const char * OPS[] = {
    "hi",
    "1",
    "2",
    "3"
};

int main(int argc, char *argv[]) {

    //define qapplication class
    QApplication app(argc, argv);

    //this line causes the error...
    EmailComposer *composerInstance = new EmailComposer;

    //declare variables
    int i;

    //create qt widgets and signal mapper
    QWidget *window = new QWidget;
    QSignalMapper *signalMapper = new QSignalMapper(window);
    QLabel *item1 = new QLabel("Status: ready...");
    QLabel *item2 = new QLabel("Number:");
    QLineEdit *item3 = new QLineEdit();
    QLabel *item4 = new QLabel("Options:");
    QComboBox *item5 = new QComboBox();
    item5->insertItem(0, "0");
    for ( i = 1 ; i <= (signed)(sizeof(OPS)/sizeof(OPS[0])) ; i++ )
        item5->insertItem(i, OPS[i-1]);
    QLabel *item6 = new QLabel("begin!");
    QTextEdit *item7 = new QTextEdit();
    QPushButton *item8 = new QPushButton("Send");

    //make item1 say "clicked!" when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(item8, "clicked!");
    QObject::connect(signalMapper, SIGNAL(mapped(QString)), item1, SLOT(setText(QString)));

    //run makenewfile() when item8 is clicked (this doesn't work because  I can't create the EmailComposer)
    QObject::connect(item8, SIGNAL(clicked()), composerInstance, SLOT(makenewfile()));

    //clear item3 when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), item3, SLOT(clear()));

    //reset item5 when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(item8, 0);
    QObject::connect(signalMapper, SIGNAL(mapped(int)), item5, SLOT(setCurrentIndex(int)));

    //clear item7 when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), item7, SLOT(clear()));

    //create layout and add widgets to layout
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(item1, 0, 0, 1, 2);
    layout->addWidget(item2, 2, 0);
    layout->addWidget(item3, 2, 1);
    layout->addWidget(item4, 3, 0);
    layout->addWidget(item5, 3, 1);
    layout->addWidget(item6, 4, 0, 1, 2);
    layout->addWidget(item7, 5, 0, 1, 2);
    layout->addWidget(item8, 6, 0, 1, 2);

    //output to screen
    window->setLayout(layout);
    window->show();

    //pass control to qt
    return app.exec();
}

emailcomposer.h

#ifndef EMAILCOMPOSER_H
#define EMAILCOMPOSER_H

#include <QObject>

class EmailComposer : public QObject {
   Q_OBJECT

   public:
      EmailComposer() {}

   public slots:
      void makenewfile() {
         system("echo \"it worked!\" > newfile.txt");
         }

   private:
      QString storedText;
};

#endif // EMAILCOMPOSER_H

I wasn't sure if Qt would allow you to create classes derived from QObjects in the same file as the main function, and based on your error I think it's not possible.  However, I may have missed something else.  I tested my code and it works.

Offline

#19 2009-05-21 02:45:08

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Hrm... on my system, I am getting "emailcomposer.h:14: error: 'system' was not declared in this scope"...

Offline

#20 2009-05-21 07:13:42

sirius
Member
From: Norway
Registered: 2008-12-25
Posts: 68

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

In emailcomposer.h you'll have to add this

#include <cstdlib>

and add std:: before system, like this

std::system("echo \"it worked!\" > newfile.txt");

.

Offline

#21 2009-05-21 13:13:50

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Right, sorry depending on how you build that is required.

I would also reccomend using this for writing to a file:

QFile data("newfile.txt");
 if (data.open(QFile::WriteOnly | QFile::Truncate)) {
     QTextStream out(&data);
     out << "it worked!";
 }

But I understand that is probably just a quick test.

Offline

#22 2009-05-21 14:44:56

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Thanks scio and sirius! My code is finally working smile

Offline

#23 2009-05-24 21:22:56

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Sorry; I spoke too fast. I've changed the makenewfile() function to sendEmail(QString) and I'd like to have sendEmail use the text in the "name" text box as its QString input so whenever the user clicks the "send" button (item8), the application will output "sending e-mail to (name)" where "name" refers to the contents of the "name" text box. Here is my main.cpp,

#include <QtGui>
#include <emailcomposer.h>

int main(int argc, char *argv[]) {

    //define qapplication class
    QApplication app(argc, argv);

    //create an instance of the email composer
    EmailComposer *composerInstance = new EmailComposer;

    //create qt widgets and signal mappers
    QWidget *window = new QWidget;
    QSignalMapper *signalMapper = new QSignalMapper(window);
    QLabel *item1 = new QLabel("Status: ready...");
    QLabel *item2 = new QLabel("Name:");
    QLineEdit *item3 = new QLineEdit("put the name here!");
    QLabel *item4 = new QLabel("Options:");
    QComboBox *item5 = new QComboBox();
    item5->insertItem(0, "a");
    item5->insertItem(1, "b");
    item5->insertItem(2, "c");
    QLabel *item6 = new QLabel("begin!");
    QTextEdit *item7 = new QTextEdit();
    QPushButton *item8 = new QPushButton("Send");

    //run sendEmail() when item8 is clicked
    QObject::connect(item8, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(item8, item3->text());
    QObject::connect(signalMapper, SIGNAL(mapped(QString)), composerInstance, SLOT(sendEmail(QString)));

    //create layout and add widgets to layout
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(item1, 0, 0, 1, 2);
    layout->addWidget(item2, 2, 0);
    layout->addWidget(item3, 2, 1);
    layout->addWidget(item4, 3, 0);
    layout->addWidget(item5, 3, 1);
    layout->addWidget(item6, 4, 0, 1, 2);
    layout->addWidget(item7, 5, 0, 1, 2);
    layout->addWidget(item8, 6, 0, 1, 2);

    //output to screen
    window->setLayout(layout);
    window->show();

    //pass control to qt
    return app.exec();
}

And here is my emailcomposer.cpp,

#ifndef EMAILCOMPOSER_H
#define EMAILCOMPOSER_H

#include <QObject>
#include <cstdlib>
#include <stdio.h>

#define STRLEN 1000

class EmailComposer : public QObject {
    Q_OBJECT

    public:
        EmailComposer() {}

    public slots:
        void sendEmail(QString text) {
            char str[STRLEN];
            memset(str, 0, STRLEN);
            sprintf(str, "echo \"sending email to %s\"", text.toUtf8().data());
            system(str);
        }
};

#endif

Unfortunately, right now, whenever the user clicks the "send" button, the application doesn't check the text box's contents and use it for sendEmail()'s input. Instead it simply uses the original text "put the name here!" Here's what it looks like when I run it:

[karam@boris emailcomposer]$ qmake -project
[karam@boris emailcomposer]$ qmake
[karam@boris emailcomposer]$ make
g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -I. -o main.o main.cpp
/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -I. emailcomposer.h -o moc_emailcomposer.cpp
g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -I. -o moc_emailcomposer.o moc_emailcomposer.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/usr/lib -o emailcomposer main.o moc_emailcomposer.o    -L/usr/lib -lQtGui -L/usr/lib -L/usr/X11R6/lib64 -pthread -lpng -lfreetype -lgobject-2.0 -lSM -lICE -pthread -pthread -lXrender -lfontconfig -lXext -lX11 -lQtCore -lz -lm -pthread -lgthread-2.0 -lrt -lglib-2.0 -ldl -lpthread
[karam@boris emailcomposer]$ ./emailcomposer
sending email to put the name here!
sending email to put the name here!
sending email to put the name here!
sending email to put the name here!
[karam@boris emailcomposer]$

I actually changed the text in the text box between clicking the send button each time, but I'm just getting the "put the name here!" output as you can see above. Do you have any ideas of how to actually use the contents of the text box? Thanks again for all your help!

Last edited by tony5429 (2009-05-24 23:16:15)

Offline

#24 2009-05-25 23:04:37

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

Does anyone have any ideas about this?

Offline

#25 2009-05-26 13:22:49

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] Simple QT Question: Connecting Widgets to Slots

setMapping is is called only once, it maps the current value of item3 to the signal of the button.  This means that no matter what you change the text to it will not use that since it was mapped before the text changed.

There are a few options here:
1) Use setMapping(item8, item3).  Then in the slot do a qobject_cast or a .inherts to make sure the object is a QLineEdit then cast it, and read the text.  This will probably be the most in keeping with what you are currently trying to do.
2) Connect item8's textChanged signal to a new slot that removes the current mapping for item8 and sets it to the current text of item3.  This seems like a lot of overhead for what it is doing.
3) Pass in the QLineEdit to the constructor of the EmailComposer and keep the reference as a member variable.  Then when the slot is called, read the text.  This would remove the need for the signal mapper, but would limit the functionality of the slot.

Offline

Board footer

Powered by FluxBB