PDA

View Full Version : Connection not properly done.... lineEdit



hipogrito
2nd April 2007, 20:52
Hi:

I'm new in this forum... trying to test QT. My background comes from MS Visual Studio.

So, I'm having some troubles figuring out this connection (signal, slot) thing... I'm more used to the onLoad, onClose, etc... of MS :o

The problem is silly. Very silly indeed.

I'm using designer. A simple main window with a slider and a spin connected.

I have a lineEdit where I want to display whatever I want depending on the value of the slider... not just the value of the slider... maybe some text saying something...

So, It seems that I have to create a new connection... which it is not working as I expected... well, it's not working at all... I guess that maybe somebody here could have a look at the code and in 2 seconds see the error... it would help me to understand how this system works. Overall it looks really nice, but I don't see 100% clear the Event handling...

Thanks!!!
Regards,
Hipogrito

GTMain.h


#ifndef GTMAIN_H
#define GTMAIN_H

#include <QMainWindow>

#include "ui_GTerm.h"

class GTMain : public QMainWindow
{
Q_OBJECT

public:
GTMain(QWidget *parent=0);

private:
Ui::GTerm ui;

private slots:
void updateText();
};

#endif




GTMain.c


#include "GTMain.h"
#include "ui_GTerm.h"

GTMain::GTMain(QWidget *parent)
: QMainWindow(parent)
{
// init the ui
ui.setupUi(this);
connect(ui.verticalSlider, SIGNAL(sliderMoved(int)), ui.lineEdit, SLOT(updateText()));

}


void GTMain::updateText()
{
ui.lineEdit->clear();
ui.lineEdit->setText("Modifing...");
ui.lineEdit->show();

// Whatever... I just want to clear the line and show the new text...
// I don't care at the beginning what to show, but I just want to be able to add whatever here in this method
}



Now the file coming from the Designer:
ui_GTerm.h


************************************************** ******************************
** Form generated from reading ui file 'GTerm.ui'
**
** Created: Mon Apr 2 15:01:26 2007
** by: Qt User Interface Compiler version 4.2.0
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
************************************************** ******************************/

#ifndef UI_GTERM_H
#define UI_GTERM_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QComboBox>
#include <QtGui/QGroupBox>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QSlider>
#include <QtGui/QSpinBox>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>

class Ui_GTerm
{
public:
QAction *actionAbout;
QAction *actionQuit;
QWidget *centralwidget;
QGroupBox *groupBox;
QSpinBox *spinBox;
QComboBox *comboBox;
QSlider *verticalSlider;
QLineEdit *lineEdit;
QMenuBar *menubar;
QMenu *menuHelp;
QMenu *menuFile;
QStatusBar *statusbar;

void setupUi(QMainWindow *GTerm)
{
GTerm->setObjectName(QString::fromUtf8("GTerm"));
actionAbout = new QAction(GTerm);
actionAbout->setObjectName(QString::fromUtf8("actionAbout"));
actionQuit = new QAction(GTerm);
actionQuit->setObjectName(QString::fromUtf8("actionQuit"));
centralwidget = new QWidget(GTerm);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
groupBox = new QGroupBox(centralwidget);
groupBox->setObjectName(QString::fromUtf8("groupBox"));
groupBox->setGeometry(QRect(30, 10, 131, 271));
spinBox = new QSpinBox(groupBox);
spinBox->setObjectName(QString::fromUtf8("spinBox"));
spinBox->setGeometry(QRect(50, 90, 55, 29));
comboBox = new QComboBox(groupBox);
comboBox->setObjectName(QString::fromUtf8("comboBox"));
comboBox->setGeometry(QRect(10, 30, 101, 26));
verticalSlider = new QSlider(groupBox);
verticalSlider->setObjectName(QString::fromUtf8("verticalSlider"));
verticalSlider->setGeometry(QRect(20, 90, 16, 160));
verticalSlider->setOrientation(Qt::Vertical);
lineEdit = new QLineEdit(centralwidget);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setGeometry(QRect(90, 400, 421, 29));
GTerm->setCentralWidget(centralwidget);
menubar = new QMenuBar(GTerm);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 633, 25));
menuHelp = new QMenu(menubar);
menuHelp->setObjectName(QString::fromUtf8("menuHelp"));
menuFile = new QMenu(menubar);
menuFile->setObjectName(QString::fromUtf8("menuFile"));
GTerm->setMenuBar(menubar);
statusbar = new QStatusBar(GTerm);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
GTerm->setStatusBar(statusbar);

menubar->addAction(menuFile->menuAction());
menubar->addAction(menuHelp->menuAction());
menuHelp->addAction(actionAbout);
menuFile->addAction(actionQuit);

retranslateUi(GTerm);

QSize size(633, 633);
size = size.expandedTo(GTerm->minimumSizeHint());
GTerm->resize(size);

QObject::connect(verticalSlider, SIGNAL(sliderMoved(int)), spinBox, SLOT(setValue(int)));
QObject::connect(spinBox, SIGNAL(valueChanged(int)), verticalSlider, SLOT(setValue(int)));
QObject::connect(verticalSlider, SIGNAL(sliderMoved(int)), lineEdit, SLOT(update()));

QMetaObject::connectSlotsByName(GTerm);
} // setupUi

void retranslateUi(QMainWindow *GTerm)
{
GTerm->setWindowTitle(QApplication::translate("GTerm", "GTERM v003", 0, QApplication::UnicodeUTF8));
actionAbout->setText(QApplication::translate("GTerm", "About", 0, QApplication::UnicodeUTF8));
actionQuit->setText(QApplication::translate("GTerm", "Quit", 0, QApplication::UnicodeUTF8));
groupBox->setTitle(QApplication::translate("GTerm", "Control 1", 0, QApplication::UnicodeUTF8));
comboBox->clear();
comboBox->addItem(QApplication::translate("GTerm", "Element01", 0, QApplication::UnicodeUTF8));
comboBox->addItem(QApplication::translate("GTerm", "Element02", 0, QApplication::UnicodeUTF8));
comboBox->addItem(QApplication::translate("GTerm", "Element03", 0, QApplication::UnicodeUTF8));
comboBox->addItem(QApplication::translate("GTerm", "Element04", 0, QApplication::UnicodeUTF8));
menuHelp->setTitle(QApplication::translate("GTerm", "Help", 0, QApplication::UnicodeUTF8));
menuFile->setTitle(QApplication::translate("GTerm", "File", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class GTerm: public Ui_GTerm {};
} // namespace Ui

#endif // UI_GTERM_H

jacek
2nd April 2007, 21:45
connect(ui.verticalSlider, SIGNAL(sliderMoved(int)), ui.lineEdit, SLOT(updateText()));
It should be:
connect( ui.verticalSlider, SIGNAL(sliderMoved(int)), this, SLOT(updateText()) );
as the updateText() slot is defined in GTMain.

You can achieve the same using automatic connections, simply by naming your slot "on_verticalSlider_sliderMoved(int)".

hipogrito
2nd April 2007, 21:49
Hi:

I found the bug:

connect(ui.verticalSlider, SIGNAL(sliderMoved(int)), this, SLOT(updateText()));

Thus the signal is sent fromt the Slider to the Main Form.


So, If I have understood this system correctly, I think that if I want to connect the Slider to the Line Edit directly and I want to do whatever I want, not the standard connections that appear in Designer, I have to create a Custom Widget in Designer which allows this communication. I guess that ideally that approach could be nicer, theoretically, but I guess that sending the connection directly to the Main Form is much faster and not very ugly.

Any other ideas of how doing this?

Thanks,

Regards
hipogrito

hipogrito
2nd April 2007, 21:51
Hello:


It should be:
connect( ui.verticalSlider, SIGNAL(sliderMoved(int)), this, SLOT(updateText()) );
as the updateText() slot is defined in GTMain.

You can achieve the same using automatic connections, simply by naming your slot "on_verticalSlider_sliderMoved(int)".

Oooooooooops, I was writing at the same time as you :D

Thank you very much for your solutions!

Let's see if within some days/weeks I could jump from the newbie to the normal forum ;)

Regards
hipogrito

jacek
2nd April 2007, 21:53
if I want to connect the Slider to the Line Edit directly and I want to do whatever I want, not the standard connections that appear in Designer, I have to create a Custom Widget in Designer which allows this communication.
Yes, you can connect only to those slots that already exist.


Any other ideas of how doing this?
I would simply use QSpinBox instead of QLineEdit.