Results 1 to 5 of 5

Thread: connect two buttons...

  1. #1
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default connect two buttons...

    Hi Guys,

    Hopefully I have a simple Question about Signals and Slot's. I wrote a little program with two buttons(A and B). So button A is an openfiledialog, which open the current dir and allows to select a file. Button B should get the filename from A and reads that file on clicked() into a string.
    At this moment I dont know how to pass the filename or position to B. I guess it should be possible with the connect() function?!? Or maybe set some global value?

    My code looks like this:

    void Mainwindow:: on_A_clicked()
    {
    QString file=QFiledialog::getOpenFileName(
    this,
    ("select a file"),
    "c:/qt/",
    ("All files (*.*)"));
    }

    void Mainwindow:: on_B_clicked()
    {
    QFile file; // <--- how to get "file" accepted here?
    file.open(QIODevice::Readonly);
    ...
    }

    Best regards,

    Olli
    Last edited by xxxollixxx; 6th December 2013 at 14:32.

  2. #2
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connect two buttons...

    Well you're going to have to store the file name somewhere, presumably in a private QString that both buttons have access to.

    What i usually do when I'm using a button for an open file dialog is set up a QLineEdit next to it and when they select the file I populate the line edit with the filename. Then when the second button is clicked you can get the filename from the line edit.

  3. #3
    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: connect two buttons...

    Store the file name in a member variable inside MainWindow, and read it from on_B_clicked().
    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.

  4. #4
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: connect two buttons...

    just for understanding: I need to set a member variable to "public:" in "Mainwindow.h"? Can it also be "private:"?

    Or is it also ok to define this in mainwindow.cpp?


    Added after 10 minutes:


    Ok I'll try this:

    QString file; //I guess this should be global?

    void Mainwindow:: on_A_clicked()
    {
    QString file=QFiledialog::getOpenFileName(
    this,
    ("select a file"),
    "c:/qt/",
    ("All files (*.*)"));
    }

    void Mainwindow:: on_B_clicked()
    {
    qDebug() << file; //empty now
    QFile file;
    file.open(QIODevice::Readonly);
    ...
    }

    Do I need to set the file name to the QString file too? If yes, please give a hint how -.-

    Olli
    Last edited by xxxollixxx; 6th December 2013 at 15:04.

  5. #5
    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: connect two buttons...

    Quote Originally Posted by xxxollixxx View Post
    just for understanding: I need to set a member variable to "public:" in "Mainwindow.h"? Can it also be "private:"?
    Yes, can be private as well. That is actually what sulliwk06 suggested.

    Quote Originally Posted by xxxollixxx View Post
    Or is it also ok to define this in mainwindow.cpp?
    mainwindow.h inside the class declaration.


    Quote Originally Posted by xxxollixxx View Post
    QString file; //I guess this should be global?
    Better as a member of Mainwindow

    Quote Originally Posted by xxxollixxx View Post
    void Mainwindow:: on_A_clicked()
    {
    QString file=QFiledialog::getOpenFileName(
    This creates a local variable with the same name, "hiding" the variable you actually want to use

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 9th May 2011, 10:38
  2. Replies: 2
    Last Post: 12th June 2010, 02:21
  3. Replies: 16
    Last Post: 16th February 2010, 13:17
  4. Replies: 4
    Last Post: 10th November 2006, 15:38
  5. Several buttons
    By Mariane in forum Newbie
    Replies: 4
    Last Post: 15th March 2006, 20:50

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.