Results 1 to 9 of 9

Thread: QKeyEvent

  1. #1
    Join Date
    Feb 2008
    Posts
    47
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post QKeyEvent

    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??



    Qt Code:
    1. QKeyEvent keyy(QEvent::FocusIn,0,Qt::KeypadModifier,"0",false,1);
    2.  
    3. if((Varname->text().length()==0) && (keyy.key() == Qt::Key_Enter))
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 22nd February 2008 at 09:59. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QKeyEvent

    J-P Nurmi

  3. #3
    Join Date
    Feb 2008
    Posts
    47
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: QKeyEvent

    Hi,
    I did like the below code.But still its not working.


    Qt Code:
    1. connect( Lineeditname, SIGNAL( returnPressed() ),
    2. this, SLOT( Display() ) );
    3.  
    4. void Classname::Display()
    5. {
    6. if(Lineeditname->text().length()==0)
    7. {}
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 22nd February 2008 at 10:29. Reason: missing [code] tags

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QKeyEvent

    What does "not working" mean? Does the slot get called?
    J-P Nurmi

  5. #5
    Join Date
    Feb 2008
    Posts
    47
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: QKeyEvent

    I m getting a msg "No such slot named display" in the command prompt and nothing is happening in the UI

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QKeyEvent

    Did you declared Display() as slot? Did you add required Q_OBJECT macro? For more details, see Signals and Slots.
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QKeyEvent

    Try out the following example by saving it as a single file called main.cpp and compiling and executing it.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget* parent=0) : QMainWindow(parent)
    8. {
    9. QWidget* cw = new QWidget;
    10. QVBoxLayout* lo = new QVBoxLayout(cw);
    11. QLabel* lb = new QLabel("Enter text below and press <b>Enter</b>");
    12. lb->setWordWrap(true);
    13. m_le = new QLineEdit;
    14. connect(m_le, SIGNAL(returnPressed()), this, SLOT(display())); // What you need
    15. m_tb = new QTextBrowser;
    16. // layout
    17. lo->addWidget(lb);
    18. lo->addWidget(m_le);
    19. lo->addWidget(m_tb);
    20. // set central widget
    21. setCentralWidget(cw);
    22. }
    23. private slots: // display() is a declared a slot!!!
    24. void display()
    25. {
    26. if (m_le->text() == "")
    27. QMessageBox::warning(this, "Empty string", "You have to enter something!");
    28. else
    29. m_tb->setText(m_le->text());
    30. }
    31. private:
    32. QLineEdit* m_le;
    33. QTextBrowser* m_tb;
    34. };
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. QApplication app(argc, argv);
    39. MainWindow mw;
    40. mw.show();
    41. return app.exec();
    42. }
    43.  
    44. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2008
    Posts
    47
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: QKeyEvent

    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:
    Qt Code:
    1. class classname : public QDialog ,Ui_dialog
    2. {
    3. Q_OBJECT
    4. }
    To copy to clipboard, switch view to plain text mode 
    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)
    Last edited by jpn; 25th February 2008 at 07:38. Reason: reformatted to look better

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QKeyEvent

    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.
    J-P Nurmi

Similar Threads

  1. what's mean of the member of QKeyEvent
    By sunote in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2007, 21:25
  2. QKeyEvent questions
    By bglidden in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2006, 06:29
  3. Posting a QKeyEvent to a QLineEdit
    By cocheci in forum Qt Programming
    Replies: 14
    Last Post: 5th June 2006, 14:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.