Qt-interest Archive, July 2007
strange slot connection behaviour
Message 1 in thread
Hello,
I'm new to qt.
I have a very stange slot connection behaviour. It compiles fine, but doesn't work at run time (I'm sure that I've connected the pushbutton with slot but nothing happened when I pressed / clicked the pushbutton). Here are the code :
This is login.h :
#ifndef LOGINFORM_H
#define LOGINFORM_H
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QSpacerItem>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
#include <QtGui/QDialog>
class LoginForm : public QDialog
{
Q_OBJECT
public:
LoginForm();
private slots:
void DoCancel();
void DoLogin();
private:
void createLayout();
QVBoxLayout *vboxLayout;
QGridLayout *gridLayout;
QHBoxLayout *hboxLayout;
QSpacerItem *spacerItem1;
QSpacerItem *spacerItem2;
QSpacerItem *spacerItem3;
QSpacerItem *spacerItem4;
QSpacerItem *spacerItem5;
QLineEdit *editUserid;
QLabel *lbUserid;
QLineEdit *editPassword;
QLabel *lbPassword;
QPushButton *cancelButton;
QPushButton *okButton;
};
#endif
and this is login.cpp :
LoginForm::LoginForm()
{
this->setWindowTitle(tr("Login form"));
this->setWindowModality(Qt::WindowModal);
createLayout();
}
void LoginForm::createLayout()
{
vboxLayout = new QVBoxLayout(this);
vboxLayout->setSpacing(6);
vboxLayout->setMargin(9);
gridLayout = new QGridLayout();
gridLayout->setSpacing(6);
gridLayout->setMargin(0);
spacerItem1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(spacerItem1, 0, 1, 1, 1);
editUserid = new QLineEdit(this);
gridLayout->addWidget(editUserid, 0, 2, 1, 1);
lbUserid = new QLabel(this);
lbUserid->setText(tr("&Userid"));
gridLayout->addWidget(lbUserid, 0, 0, 1, 1);
editPassword = new QLineEdit(this);
gridLayout->addWidget(editPassword, 1, 2, 1, 1);
lbPassword = new QLabel(this);
lbPassword->setText(tr("&Password"));
gridLayout->addWidget(lbPassword, 1, 0, 1, 1);
spacerItem2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(spacerItem2, 1, 1, 1, 1);
vboxLayout->addLayout(gridLayout);
spacerItem3 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
vboxLayout->addItem(spacerItem3);
hboxLayout = new QHBoxLayout();
hboxLayout->setSpacing(6);
hboxLayout->setMargin(0);
spacerItem4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
hboxLayout->addItem(spacerItem4);
cancelButton = new QPushButton(this);
cancelButton->setText(tr("&Cancel"));
connect(cancelButton,SIGNAL(clicked()),this,SLOT(DoCancel()));
okButton = new QPushButton(this);
okButton->setText(tr("&Ok"));
connect(okButton,SIGNAL(clicked()),this,SLOT(DoLogin()));
hboxLayout->addWidget(okButton);
hboxLayout->addWidget(cancelButton);
spacerItem5 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
hboxLayout->addItem(spacerItem5);
vboxLayout->addLayout(hboxLayout);
lbUserid->setBuddy(editUserid);
lbPassword->setBuddy(editPassword);
}
void LoginForm::DoLogin()
{
string result;
// some login process
if (result != "") {
accept();
}
else
{
QMessageBox::critical(this,tr("login error"),tr("Invalied Userid / password !"),QMessageBox::Ok);
}
}
void LoginForm::DoCancel()
{
QMessageBox::critical(this,tr("Debug"),tr("Cancelled !"),QMessageBox::Ok);
reject();
}
The OK button work as expected. The CANCEL button behave strangely, it seem as if the DoCancel slot is not connected with cancelButton (I clicked the cancelbutton and nothing happened, seem like the flow of code execution never reach this part. I've tried to replace the message box with loging to file but no log file generated). May some QT macro (Q_OBJECT) ruined my slot connection ?. Or, Am I missing something here ?.
Thanks in advance for your reply,
regards,
si_sol
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
--
[ signature omitted ]
Message 2 in thread
On 7/23/07, SI SOL <si_sol@xxxxxxxxx> wrote:
> Hello,
>
> I'm new to qt.
> I have a very stange slot connection behaviour. It compiles fine, but doesn't work at run time (I'm sure that I've connected the pushbutton with slot but nothing happened when I pressed / clicked the pushbutton). Here are the code :
>
> This is login.h :
>
> #ifndef LOGINFORM_H
>
> #define LOGINFORM_H
>
>
> #include <QtGui/QAction>
>
> #include <QtGui/QApplication>
>
> #include <QtGui/QButtonGroup>
>
> #include <QtGui/QGridLayout>
>
> #include <QtGui/QHBoxLayout>
>
> #include <QtGui/QLabel>
>
> #include <QtGui/QLineEdit>
>
> #include <QtGui/QPushButton>
>
> #include <QtGui/QSpacerItem>
>
> #include <QtGui/QVBoxLayout>
>
> #include <QtGui/QWidget>
>
> #include <QtGui/QDialog>
>
>
> class LoginForm : public QDialog
>
> {
>
> Q_OBJECT
>
>
> public:
>
> LoginForm();
>
>
> private slots:
>
> void DoCancel();
>
> void DoLogin();
>
>
> private:
>
> void createLayout();
>
>
> QVBoxLayout *vboxLayout;
>
> QGridLayout *gridLayout;
>
> QHBoxLayout *hboxLayout;
>
> QSpacerItem *spacerItem1;
>
> QSpacerItem *spacerItem2;
>
> QSpacerItem *spacerItem3;
>
> QSpacerItem *spacerItem4;
>
> QSpacerItem *spacerItem5;
>
> QLineEdit *editUserid;
>
> QLabel *lbUserid;
>
> QLineEdit *editPassword;
>
> QLabel *lbPassword;
>
> QPushButton *cancelButton;
>
> QPushButton *okButton;
>
>
> };
>
> #endif
>
>
> and this is login.cpp :
>
> LoginForm::LoginForm()
>
> {
>
> this->setWindowTitle(tr("Login form"));
>
> this->setWindowModality(Qt::WindowModal);
>
> createLayout();
>
>
> }
>
>
> void LoginForm::createLayout()
>
> {
>
> vboxLayout = new QVBoxLayout(this);
>
> vboxLayout->setSpacing(6);
>
> vboxLayout->setMargin(9);
>
> gridLayout = new QGridLayout();
>
> gridLayout->setSpacing(6);
>
> gridLayout->setMargin(0);
>
>
> spacerItem1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
>
> gridLayout->addItem(spacerItem1, 0, 1, 1, 1);
>
>
> editUserid = new QLineEdit(this);
>
> gridLayout->addWidget(editUserid, 0, 2, 1, 1);
>
>
> lbUserid = new QLabel(this);
>
> lbUserid->setText(tr("&Userid"));
>
> gridLayout->addWidget(lbUserid, 0, 0, 1, 1);
>
>
> editPassword = new QLineEdit(this);
>
> gridLayout->addWidget(editPassword, 1, 2, 1, 1);
>
>
> lbPassword = new QLabel(this);
>
> lbPassword->setText(tr("&Password"));
>
> gridLayout->addWidget(lbPassword, 1, 0, 1, 1);
>
>
> spacerItem2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
>
> gridLayout->addItem(spacerItem2, 1, 1, 1, 1);
>
> vboxLayout->addLayout(gridLayout);
>
>
> spacerItem3 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
>
> vboxLayout->addItem(spacerItem3);
>
>
> hboxLayout = new QHBoxLayout();
>
> hboxLayout->setSpacing(6);
>
> hboxLayout->setMargin(0);
>
> spacerItem4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
>
> hboxLayout->addItem(spacerItem4);
>
>
> cancelButton = new QPushButton(this);
>
> cancelButton->setText(tr("&Cancel"));
>
> connect(cancelButton,SIGNAL(clicked()),this,SLOT(DoCancel()));
>
>
> okButton = new QPushButton(this);
>
> okButton->setText(tr("&Ok"));
>
> connect(okButton,SIGNAL(clicked()),this,SLOT(DoLogin()));
>
>
> hboxLayout->addWidget(okButton);
>
> hboxLayout->addWidget(cancelButton);
>
>
> spacerItem5 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
>
> hboxLayout->addItem(spacerItem5);
>
> vboxLayout->addLayout(hboxLayout);
>
>
> lbUserid->setBuddy(editUserid);
>
> lbPassword->setBuddy(editPassword);
> }
>
>
> void LoginForm::DoLogin()
>
> {
>
> string result;
>
>
> // some login process
>
> if (result != "") {
>
> accept();
>
> }
>
> else
>
> {
>
> QMessageBox::critical(this,tr("login error"),tr("Invalied Userid / password !"),QMessageBox::Ok);
>
> }
>
> }
>
>
> void LoginForm::DoCancel()
>
> {
>
> QMessageBox::critical(this,tr("Debug"),tr("Cancelled !"),QMessageBox::Ok);
>
> reject();
>
> }
Unrelated to your question, but you might want to consider using
Designer if you've been hand-coding forms.
--
[ signature omitted ]
Message 3 in thread
>----- Original Message ----
>From: SI SOL <si_sol@xxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Sent: Tuesday, July 24, 2007 8:23:50 AM
>Subject: strange slot connection behaviour
>The OK button work as expected. The CANCEL button behave strangely, it seem as if the >DoCancel slot is not connected with cancelButton (I clicked the cancelbutton and nothing >happened, seem like the flow of code execution never reach this part. I've tried to replace the >message box with loging to file but no log file generated). May some QT macro >(Q_OBJECT) ruined my slot connection ?. Or, Am I missing something here ?.
Other fact that make me puzzled : exe with debuging turn on work as expected. Exe with debuging turn off have unexpected behaviour.
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
--
[ signature omitted ]
Message 4 in thread
>----- Original Message ----
>From: Andrew Medico <a.medico@xxxxxxxxx>
>To: SI SOL <si_sol@xxxxxxxxx>
>Cc: qt-interest@xxxxxxxxxxxxx
>Sent: Tuesday, July 24, 2007 10:17:04 AM
>Subject: Re: strange slot connection behaviour
>
>
>Unrelated to your question, but you might want to consider using
>Designer if you've been hand-coding forms.
>
>--
>Andrew Medico <a.medico@xxxxxxxxx>
Thanks for your reply. I've solved this problem. Finally I found the cause of the problem : the target directory of meta object compiler during custom build process from visual studio (MOC.EXE blahblah -o target_dir/filename) point to another directory. The nmake.exe still pick the old file from previous build.
Regarding designer, Qt version 4.xx seem to remove the ability to create custom slot. Other consideration, I'm afraid of the posible overhead that may be produced by the GUI compiler (uic.exe).
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
--
[ signature omitted ]
Message 5 in thread
>
> Thanks for your reply. I've solved this problem. Finally I found the
cause
> of the problem : the target directory of meta object compiler during
> custom build process from visual studio (MOC.EXE blahblah -o
> target_dir/filename) point to another directory. The nmake.exe still
pick
> the old file from previous build.
>
> Regarding designer, Qt version 4.xx seem to remove the ability to
create
> custom slot. Other consideration, I'm afraid of the posible overhead
that
> may be produced by the GUI compiler (uic.exe).
>
QT 4.XX change the basic method of creating designer created widgets.
You used to have 2 choices, create a base class to do the drawing and
derive from it to do the actual implementation... Which in my opening
very few people did, since there is some overhead in the coding to do
so.
The other option, was to create the complete working class in designer..
Which again, IMHO, most people did in QT 3.X... However, it was much
harder to reuse the drawing.. but it was "easier"
In 4.XX, they limit you to the former, however, once you get over the
hurdle of deriving from the generated base class, you will find it's a
very easy transition.
I always hated using designer for coding, and only used it to "update
the headers" the uic read a XML file that was only updated by reading in
the .ui.h file...
As to the overhead, I think you will find, that the number of widgets
will be the same, the number of layouts will also be the same....
However, you will find that there is some initialization code that you
might find unnecessary.. in reality... you probably need it.
Svott
--
[ signature omitted ]