PDA

View Full Version : QKeyEvent



sonuani
22nd February 2008, 09:48
hi,
If my QLineEdit text length is 0 and if i press enter.Then a message has to be dispayed.
I tried the below code.Its not working.
Whats wrong with the code??




QKeyEvent keyy(QEvent::FocusIn,0,Qt::KeypadModifier,"0",false,1);

if((Varname->text().length()==0) && (keyy.key() == Qt::Key_Enter))
{
}

jpn
22nd February 2008, 10:01
See QLineEdit::returnPressed().

sonuani
22nd February 2008, 10:18
Hi,
I did like the below code.But still its not working.



connect( Lineeditname, SIGNAL( returnPressed() ),
this, SLOT( Display() ) );

void Classname::Display()
{
if(Lineeditname->text().length()==0)
{}
}

jpn
22nd February 2008, 10:30
What does "not working" mean? Does the slot get called?

sonuani
22nd February 2008, 12:16
I m getting a msg "No such slot named display" in the command prompt and nothing is happening in the UI

jpn
22nd February 2008, 12:45
Did you declared Display() as slot? Did you add required Q_OBJECT macro? For more details, see Signals and Slots (http://doc.trolltech.com/4.3/signalsandslots.html).

momesana
22nd February 2008, 15:08
Try out the following example by saving it as a single file called main.cpp and compiling and executing it.



#include <QtGui>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent=0) : QMainWindow(parent)
{
QWidget* cw = new QWidget;
QVBoxLayout* lo = new QVBoxLayout(cw);
QLabel* lb = new QLabel("Enter text below and press <b>Enter</b>");
lb->setWordWrap(true);
m_le = new QLineEdit;
connect(m_le, SIGNAL(returnPressed()), this, SLOT(display())); // What you need
m_tb = new QTextBrowser;
// layout
lo->addWidget(lb);
lo->addWidget(m_le);
lo->addWidget(m_tb);
// set central widget
setCentralWidget(cw);
}
private slots: // display() is a declared a slot!!!
void display()
{
if (m_le->text() == "")
QMessageBox::warning(this, "Empty string", "You have to enter something!");
else
m_tb->setText(m_le->text());
}
private:
QLineEdit* m_le;
QTextBrowser* m_tb;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}

#include "main.moc"

sonuani
25th February 2008, 06:35
hi,
I tried adding Q_OBJECT to my program.
I got the below linker errors.Wt is wrong here?Am i suppose to add anything??

code:

class classname : public QDialog ,Ui_dialog
{
Q_OBJECT
}

Linker Errors:

classname .obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall classname ::metaObject(void)const " (?metaObject@classname @@UBEPBUQMetaObject@@XZ)

classname .obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall classname ::qt_metacast(char const *)" (?qt_metacast@classname @@UAEPAXPBD@Z)

classname .obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall classname ::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@classname @@UAEHW4Call@QMetaObject@@HPAPAX@Z)

jpn
25th February 2008, 07:38
You must re-run qmake after adding or removing Q_OBJECT. It will generate required moc rules to your makefile (or visual studio project file). Also, make sure the header file you added Q_OBJECT macro is listed in .pro file.