Results 1 to 15 of 15

Thread: how to import a file into qt when double clicking a file.

  1. #1
    Join Date
    Aug 2010
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to import a file into qt when double clicking a file.

    I mean this:
    If I have a program called A,and I set A as the default program for opening mp3 files in Linux,how can my program know it when the user opens a mp3 file in Linux desktop environment?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to import a file into qt when double clicking a file.

    This has nothing to do with your application (Qt). It's a setting of your desktop environment and you have to set the setting there.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to import a file into qt when double clicking a file.

    @Lykurg: i think that what he meant to ask is: how the Linux OS is sending the file (name and path) to the associated qt application so that the programmer can open that particular file. (if that was the question - i don't know the answer )

  4. The following user says thank you to Zlatomir for this useful post:

    berlinud (16th September 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to import a file into qt when double clicking a file.

    Quote Originally Posted by Zlatomir View Post
    @Lykurg: i think that what he meant to ask is: how the Linux OS is sending the file (name and path) to the associated qt application so that the programmer can open that particular file. (if that was the question - i don't know the answer )
    Ok, if so, you have to parse the command line arguments in your main function and react on them. Normaly the file name is just append to the executable like: yourapp filename.mp3.

  6. The following 2 users say thank you to Lykurg for this useful post:

    berlinud (16th September 2010), Zlatomir (16th September 2010)

  7. #5
    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 import a file into qt when double clicking a file.

    Although suffixes will mostly do the job, a more correct way is to use mime types.

    edit: ignore, I read the previous post wrong
    Last edited by tbscope; 16th September 2010 at 15:44.

  8. #6
    Join Date
    Aug 2010
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to import a file into qt when double clicking a file.

    You are definitely right Zlatomir!
    That is exactly what I mean.

  9. #7
    Join Date
    Aug 2010
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to import a file into qt when double clicking a file.

    Would you mind explaining it a little more detail?
    Since I am new in Linux programming.

  10. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to import a file into qt when double clicking a file.

    Quote Originally Posted by Lykurg View Post
    Ok, if so, you have to parse the command line arguments in your main function and react on them. Normaly the file name is just append to the executable like: yourapp filename.mp3.
    I thought that might be the way, but i wasn't sure about that, thanks.

  11. #9
    Join Date
    Aug 2010
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to import a file into qt when double clicking a file.

    what data will be transfered to me as the argument of main?
    is that an absolute path?

  12. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to import a file into qt when double clicking a file.

    Quote Originally Posted by berlinud View Post
    is that an absolute path?
    I don't know, and you also can't know. Your app has to check all possibilities. Simple have a look on the value of argv (e.g. using qDebug()) and try to determain how you can handle the parameters.

  13. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to import a file into qt when double clicking a file.

    Here is a test project:
    Qt Code:
    1. #include <QtGui>
    2. #include <QStringList>
    3.  
    4. int main(int argc, char** argv){
    5.  
    6. QApplication a(argc, argv);
    7.  
    8. QStringList l = qApp->arguments(); // qApp is a macro to QApplication instance and the arguments() returns a QStringList with the arguments
    9.  
    10. t.setText(l.join("\n"));
    11. t.show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    LE: I'm on win now and the first one is the full path and name of the application
    Last edited by Zlatomir; 16th September 2010 at 16:15.

  14. The following user says thank you to Zlatomir for this useful post:

    berlinud (16th September 2010)

  15. #12
    Join Date
    Aug 2010
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to import a file into qt when double clicking a file.

    I tested it.
    $ ./TestForMainArg ggg fff fff fff fff
    the result is this:

    ./TestForMainArg
    ggg
    fff
    fff
    fff
    fff

  16. #13
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to import a file into qt when double clicking a file.

    create a text file rename it with .bla extension and create the association and see how the actual double click acts. (it should send the path too)

  17. The following user says thank you to Zlatomir for this useful post:

    berlinud (16th September 2010)

  18. #14
    Join Date
    Aug 2010
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to import a file into qt when double clicking a file.

    When I turn the test program as the default program for mp3;
    it shows that the arguments are transfered into the program as absolute path.

  19. #15
    Join Date
    Aug 2010
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to import a file into qt when double clicking a file.

    yes you are right

Similar Threads

  1. QT Sqlite database import/ export csv file
    By logesh in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2010, 05:35
  2. Double Clicking Pro File
    By BalaQT in forum Installation and Deployment
    Replies: 2
    Last Post: 18th November 2009, 05:23
  3. QListView icon double clicking
    By been_1990 in forum Qt Programming
    Replies: 8
    Last Post: 28th April 2009, 20:05
  4. import pdf file as image
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2008, 06:43
  5. Double clicking with QTableView
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2006, 06:22

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.