Results 1 to 7 of 7

Thread: how to get user inputted info into my code?

  1. #1
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default how to get user inputted info into my code?

    How can I get user inputted information into my code?

    I am making a gui with a qwtplot and I want the user to be able to fill in a text box with a file path. When they push enter I want that path to be transfered into my code so that I can load and plot it. I don't even really know where to start, I have been looking through the qt examples but I can't find one that I can work with.

    Do I need to make a custom signal/slot function? If so could anyone point me to an example or show me some code syntax on how it would work?

    thanks a lot!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to get user inputted info into my code?

    Try to get familiar with the components you want to use.
    Read their documentation carefully.

    http://doc.qt.nokia.com/4.7/qlineedi...#returnPressed

  3. #3
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get user inputted info into my code?

    Thanks for the quick reply.

    So I have been reading everything I can find but I'm still missing something.

    I have some working code but I'm still unsure how I can get the user inputted text into a variable so that I can load the file.
    this is what I have so far:

    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3. #include <QVBoxLayout>
    4. #include <QLineEdit>
    5. #include <QPushButton>
    6. #include "qobjectdefs.h"
    7.  
    8. class myCanvas : public QWidget{
    9. Q_OBJECT
    10. public:
    11. myCanvas (QWidget * parent = 0);
    12. public Q_SLOTS:
    13. void copyText(const QString & text);// maybe I need to create a slot that copies the text....
    14. };
    15.  
    16. myCanvas::myCanvas(QWidget *parent):QWidget(parent){
    17. QLineEdit * lineedit = new QLineEdit;
    18. QPushButton * pushbutton = new QPushButton("Enter");
    19.  
    20. connect(pushbutton, SIGNAL(clicked()), lineedit, SLOT(copyText()));
    21.  
    22. QVBoxLayout * layout = new QVBoxLayout;
    23. layout->addWidget(lineedit);
    24. layout->addWidget(pushbutton);
    25. setLayout(layout);
    26. }
    27.  
    28. void myCanvas::copyText (const QString & text)
    29. {
    30. ////I don't know....
    31.  
    32. }
    33.  
    34. int main (int argc, char * argv[])
    35. {
    36. QApplication app (argc, argv);
    37. myCanvas bob;
    38. bob.show();
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 


    Thanks for the help

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to get user inputted info into my code?

    You can't connect signals and slots where the slot has more arguments than the signal.
    This would mean that some data needs to come from thin air.

    This means that the following is not possible:
    Qt Code:
    1. connect(pushbutton, SIGNAL(clicked()), lineedit, SLOT(copyText()));
    To copy to clipboard, switch view to plain text mode 

    Instead, create a slot like this:

    Qt Code:
    1. public slots:
    2. void slotButtonClicked();
    To copy to clipboard, switch view to plain text mode 

    This slots has maximum the same amount and the same type of arguments as the signal. In this case the clicked() signal of the button.
    To connect them, write:

    Qt Code:
    1. connect(pushbutton, SIGNAL(clicked()), this, SLOT(slotButtonClicked());
    To copy to clipboard, switch view to plain text mode 

    Make sure that at least the QLineEdit is available in the whole myCanvas class.
    But a general rule is to manage all your pointers, otherwise you might get into trouble.

    Thus in your class definition do this:
    Qt Code:
    1. private:
    2. QLineEdit* lineedit;
    3. QPushButton* pushbutton;
    To copy to clipboard, switch view to plain text mode 

    In the implementation you'll get something like:

    Qt Code:
    1. myCanvas::myCanvas(QWidget* parent) : QWidget(parent) // You might also want to pass the flags to the base class.
    2. {
    3. ... // other code maybe
    4.  
    5. lineedit = new QLineEdit(this);
    6. pushbutton = new QPushButton("Enter", this);
    7.  
    8. ... // other code maybe, like the connection of the signal and slot, the layout, ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now that the signal/slot connection is right and the widgets are available in the whole class, you can implement the slot:

    Qt Code:
    1. void myCanvas::slotButtonClicked()
    2. {
    3. QString theText = lineedit->text();
    4. }
    To copy to clipboard, switch view to plain text mode 

    You use the line edit inside the slot connected to the button clicked signal.

  5. The following user says thank you to tbscope for this useful post:

    kja (16th November 2010)

  6. #5
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get user inputted info into my code?

    Thanks a lot, I think I'm now beginning to understand how these work.
    Unfortionatly I think I have another problem with how my program is running. I am getting three errors that have something to do with Q_OBJECT

    Qt Code:
    1. #ifndef SLOTS_H
    2. #define SLOTS_H
    3.  
    4. #include <QtGui>
    5. #include <QApplication>
    6. #include <QVBoxLayout>
    7. #include <QLineEdit>
    8. #include <QPushButton>
    9.  
    10. class myCanvas : public QWidget{
    11. Q_OBJECT
    12. QLineEdit *lineedit;
    13. QPushButton *pushbutton;
    14. public:
    15. myCanvas (QWidget * parent = 0);
    16. public Q_SLOTS:
    17. void slotButtonClicked();
    18. };
    19. #endif SLOTS_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "slots.h"
    2.  
    3. myCanvas::myCanvas(QWidget *parent):QWidget(parent){
    4. lineedit = new QLineEdit(this);
    5. pushbutton = new QPushButton("Enter", this);
    6. connect(pushbutton, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
    7. QVBoxLayout * layout = new QVBoxLayout;
    8. layout->addWidget(lineedit);
    9. layout->addWidget(pushbutton);
    10. setLayout(layout);
    11. }
    12. void myCanvas::slotButtonClicked()
    13. {
    14. QString theText = lineedit->text();
    15. }
    16. int main (int argc, char * argv[])
    17. {
    18. QApplication app (argc, argv);
    19. myCanvas bob;
    20. bob.show();
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    errors :
    Qt Code:
    1. 1>Compiling...
    2. 1>connectWorking.cpp
    3. 1>Linking...
    4. 1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall myCanvas::metaObject(void)const " (?metaObject@myCanvas@@UBEPBUQMetaObject@@XZ)
    5. 1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall myCanvas::qt_metacast(char const *)" (?qt_metacast@myCanvas@@UAEPAXPBD@Z)
    6. 1>connectWorking.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall myCanvas::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@myCanvas@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    7. 1>\funcLoadData.exe : fatal error LNK1120: 3 unresolved externals
    8. 1>Build log was saved at "file://c:...\funcLoadData\Debug\BuildLog.htm"
    9. 1>funcLoadData - 4 error(s), 0 warning(s)
    10. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    To copy to clipboard, switch view to plain text mode 

    If i comment out Q_OBJECT the program runs but the connection doesn't work

    "Object::connect: No such slot QWidget::slotButtonClicked() in c:\.....connectworking.cpp:114"

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to get user inputted info into my code?

    Re-run qmake then rebuild the project.

  8. The following user says thank you to ChrisW67 for this useful post:

    kja (16th November 2010)

  9. #7
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get user inputted info into my code?

    Ahh finally, it works!


    The problem was that my .moc file was not being generated because I didn't have the correct properties for my .h file.

    All I needed to do was edit the command line and additional dependencies, then add the moc file to my project. Ahem, like I said, I'm pretty new to Qt.


    Thanks for all your help tbscope!

Similar Threads

  1. Hiding code from the user
    By Plixil in forum Newbie
    Replies: 4
    Last Post: 19th July 2010, 11:36
  2. www.qtsearch.info
    By rajesh in forum Qt Programming
    Replies: 1
    Last Post: 6th October 2009, 20:40
  3. Replies: 2
    Last Post: 27th November 2008, 10:16
  4. version info
    By roxton in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2008, 14:22
  5. Info about QSystemTrayIcon
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2006, 14:20

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.