Results 1 to 5 of 5

Thread: Use Push Button to change a variable

  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Use Push Button to change a variable

    I am trying to make a program that pulls text from a text file indicated by the user (typing the file path in a lineEdit) and displays it on a text edit widget. Here's what I have so far:

    #include <QApplication>
    #include <QtGui>
    #include <QtCore>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWidget *window = new QWidget;
    window->setWindowTitle("Text Reader");

    QGridLayout *layout = new QGridLayout;

    QTextEdit *textbox = new QTextEdit();
    QLineEdit *enterFile = new QLineEdit();
    QPushButton *goButton = new QPushButton("Ok");
    QPushButton *quitButton = new QPushButton("Quit");

    layout->addWidget(textbox, 0,0,1,1);
    layout->addWidget(enterFile, 1,0,1,1);
    layout->addWidget(goButton, 2,0,1,1);
    layout->addWidget(quitButton, 3,0,1,1);

    window->setLayout(layout);

    QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

    //
    //STUFF TO BE ADDED
    //

    window->show();
    return app.exec(); //Starts main event loop
    }

    This is just the shell of things, and I'm learning as I add things in. I'll add in all the stuff for opening the file and storing it's contents into a QString later, but how do I take the text from 'enterFile' (a QLineEdit), and store that into a QString? I'll end up using that string to open up a file and load its text onto the textEdit widget. I've tried using a SIGNAL/SLOT mechanism thing, but it doesn't seem to do anything.

    Also, I'm totally new to qt and object oriented programming. The only experience I have is working with console applications in VC++ (though I know the basics there well enough)

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Use Push Button to change a variable

    Have a look at this, this is one way to do it.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QtCore>
    4.  
    5. class QTextEditLoader : public QObject // Added <<<<<<<<<<<<<<<<<<<<
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. explicit QTextEditLoader(QTextEdit *textEdit, QLineEdit * filename)
    11. : QObject(textEdit)
    12. , parent(textEdit)
    13. , fileName(filename)
    14. {
    15. ;
    16. }
    17.  
    18. public slots:
    19. void loadTextFromFile(void)
    20. {
    21. QString text("<Error, Unable to open file>");
    22. QFile file(fileName->text());
    23.  
    24. if(file.open(file.ReadOnly | file.Text))
    25. {
    26. text = file.readAll();
    27. if(text.isEmpty())
    28. text = "<Error, Empty/Non-Text File>";
    29. file.close();
    30. }
    31.  
    32. parent->setText(text);
    33. }
    34.  
    35. private:
    36. QTextEdit *parent;
    37. QLineEdit *fileName;
    38. };
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. QApplication app(argc, argv);
    43. QWidget *window = new QWidget;
    44. window->setWindowTitle("Text Reader");
    45.  
    46. QGridLayout *layout = new QGridLayout;
    47.  
    48. QTextEdit *textbox = new QTextEdit();
    49. QLineEdit *enterFile = new QLineEdit();
    50. QTextEditLoader *loader = new QTextEditLoader(textbox, enterFile); // Added <<<<<<<<<<<<<<<<<<<<
    51. QPushButton *goButton = new QPushButton("Ok");
    52. QPushButton *quitButton = new QPushButton("Quit");
    53.  
    54. layout->addWidget(textbox, 0,0,1,1);
    55. layout->addWidget(enterFile, 1,0,1,1);
    56. layout->addWidget(goButton, 2,0,1,1);
    57. layout->addWidget(quitButton, 3,0,1,1);
    58.  
    59. window->setLayout(layout);
    60.  
    61. QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    62. QObject::connect(goButton, SIGNAL(clicked()), loader, SLOT(loadTextFromFile())); // Added <<<<<<<<<<<<<<<<<<<<
    63.  
    64. //
    65. //STUFF TO BE ADDED
    66. //
    67.  
    68. window->show();
    69. return app.exec(); //Starts main event loop
    70. }
    71.  
    72. #include "Main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use Push Button to change a variable

    Quote Originally Posted by Qem1st View Post
    but how do I take the text from 'enterFile' (a QLineEdit), and store that into a QString
    Already very thorougly answered but just to emphasis that the most reduced answer to your question is
    Qt Code:
    1. QString fileName = enterFile->text();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  4. #4
    Join Date
    Jan 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use Push Button to change a variable

    I've got everything fixed up, and have everything set up to get the file name and to open the file, but when I try to use this line of code,

    QString line = Stream.readAll();

    or this,

    QString line = Stream.readLine(0);

    'line' is empty. I've verified that it opened the file up without a problem, but somehow it thinks it's empty. The file is a text file with some random crap that I generated in notepad.

    Why can't I get anything?

  5. #5
    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: Use Push Button to change a variable

    Why can't I get anything?
    How can we possibly know? How have you opened the file? Have you already read anything form it? Is it the file you think you have opened or another? What is the relationship between the file you have opened and "Stream"?

Similar Threads

  1. [QT]Push button-no action
    By nqn in forum Newbie
    Replies: 4
    Last Post: 30th May 2010, 20:08
  2. Hareware Push Button
    By mickeyk191 in forum Newbie
    Replies: 2
    Last Post: 26th January 2010, 13:41
  3. How to change text color of push button?
    By augusbas in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2009, 11:32
  4. shape of push button
    By Seema Rao in forum Qt Programming
    Replies: 23
    Last Post: 2nd April 2008, 02:05
  5. Push Button problem!!
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2006, 17:31

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.