Results 1 to 12 of 12

Thread: how to connect an openfile signal to a widget slot?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1

    Default Re: how to connect an openfile signal to a widget slot?

    if you use QtCreator to create your form (top level QWidget) and use creator to add your menu options, the signals you are looking
    for will be referenceable via the 'ui' pointer as shown below. supply your own file open handlers such as mine 'handleFileOpenClick'.
    Creator will setup a file called (in my case) ui_mainwindow.h which can be found in your build directory. There you will see all your
    designer generated widgets.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5.  
    6. settings = new QSettings(this);
    7.  
    8. ui->setupUi(this);
    9.  
    10. connect ( ui->actionOpen, SIGNAL(triggered()), this, SLOT(handleFileOpenClick()));
    11. connect ( ui->actionSave, SIGNAL(triggered()), this, SLOT(handleFileSaveClick()));
    12. connect ( ui->actionNew, SIGNAL(triggered()), this, SLOT(handleNewClick()));
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::handleFileOpenClick()
    2. {
    3. QString prevFilePath=QDir::homePath();
    4. QString fileName ;
    5.  
    6. if ( settings->contains(_keyLastFilePath))
    7. {
    8. prevFilePath = settings->value(_keyLastFilePath).toString();
    9. }
    10.  
    11. fileName = QFileDialog::getOpenFileName(this,
    12. tr("Open SCXML Input File"), prevFilePath, tr("SCXML Files (*.scxml)"));
    13.  
    14.  
    15.  
    16. settings->setValue(_keyLastFilePath, fileName);
    17.  
    18. project = new SMProject(ui->graphicsView);
    19. project->readInputFile(fileName);
    20. }
    21.  
    22.  
    23. void MainWindow::handleFileSaveClick()
    24. {
    25. if ( project == NULL) return;
    26.  
    27. QString prevFilePath=QDir::homePath();
    28. QString fileName ;
    29.  
    30. if ( settings->contains(_keyLastFilePath))
    31. {
    32. prevFilePath = settings->value(_keyLastFilePath).toString();
    33. }
    34.  
    35. fileName = QFileDialog::getSaveFileName(this,
    36. tr("Open SCXML Input File"), prevFilePath, tr("SCXML Files (*.scxml)"));
    37.  
    38. settings->setValue(_keyLastFilePath, fileName);
    39.  
    40. project->save(fileName);
    41. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    thanks,
    I am going to test these suggestions straight away.

  3. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    Hi,

    thanks to the great documentation/tutorials/fora,
    I managed to set up the menubar with QAction. Now I face this problem

    Qt Code:
    1. error: ‘my2dPlot* MyWidget::mainPlot’ is private
    To copy to clipboard, switch view to plain text mode 
    when i try to add this line to the openfile action

    Qt Code:
    1. widget->mainPlot->plotMainCurve(fileName);
    To copy to clipboard, switch view to plain text mode 

    -widget is the central widget of the mainWindow
    -mainPlot is a class that I have created to plot 2d plots
    -plotMainCurve is the method to plot the selected data file


    This is seems to be a typical hierarchy error. But how can i overcome it and call the function to plot the curve of a specific data file (filename= "data.txt")?

  4. #4
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    before it was:


    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5. private:
    6. my2dPlot *mainPlot;
    7. ....
    8. {
    To copy to clipboard, switch view to plain text mode 


    and now i moved the mainPlot member to the public:

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5. my2dPlot *mainPlot;
    6. ....
    7. {
    To copy to clipboard, switch view to plain text mode 

    I don't know if it is a real solution and/or safe thing to do. But how else could i call the plotMainCurve(filename) of the my2dPlot class form the mainwindow?

  5. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect an openfile signal to a widget slot?

    Quote Originally Posted by fatecasino View Post
    I don't know if it is a real solution and/or safe thing to do. But how else could i call the plotMainCurve(filename) of the my2dPlot class form the mainwindow?
    you could write a getter method:

    Qt Code:
    1. my2dPlot* Get2DPlot() {return my2dplot;} const;
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: how to connect an openfile signal to a widget slot?

    yeah, but I don't want to receive the plot. The plot belongs to the my2dPlot class. I just want to send the datafile that I want to print. As you see the hierarchy goes:


    Qt Code:
    1. widget->mainPlot->plotMainCurve(fileName);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to connect an openfile signal to a widget slot?

    What about
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5. public slots:
    6. void plotFromFile( const QString& file ){
    7. this->mainPlot->plotMainCurve(file);
    8. }
    9. private:
    10. my2dPlot *mainPlot;
    11. ....
    12. {
    To copy to clipboard, switch view to plain text mode 
    ?

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

    fatecasino (14th December 2010)

  9. #8
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to connect an openfile signal to a widget slot?

    @stampede: it worked like a charm!!
    thank you very much..I always really confused with the usage of "this"
    What is the actual difference between the two lines??
    Qt Code:
    1. this->mainPlot->plotMainCurve(file);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. widget->mainPlot->plotMainCurve(fileName);
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect an openfile signal to a widget slot?

    "this" is a pointer to the object which "posesses" the current method.

    the difference is that the second line "widget->mainPlot..." works only if "mainPlot" is public. That's why I recommended to create a getter. The following would have worked too:
    Qt Code:
    1. widget->Get2DPlot()->plotMainCurve(file)
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to connect an openfile signal to a widget slot?

    What is the actual difference between the two lines??
    You use "this" keyword from class methods when you refer to actualy used class instance ( the object that "calls" the method or access the class member ).
    Of course you have access to all members of the class in this case.

    Here
    widget->mainPlot->plotMainCurve(fileName);
    you use some object "widget" to call the method. You can access only public members of the "widget" object ( unless you are a friend of a class )

    This basic knowledge is mandatory in C++ programming, I really recommend some reading before going into qt programming ( you can find some useful info here: http://www.qtcentre.org/threads/29-W...ourite-C-books )

Similar Threads

  1. Replies: 2
    Last Post: 15th September 2010, 00:54
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. How to connect signal/slot in QItemEditorFactory?
    By yyalli in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2010, 14:56
  4. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 19:38
  5. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 22:55

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.