how to connect an openfile signal to a widget slot?
hi,
I want to create a simple mainwindow which is consisted of a menubar for openfile and a plotting area (qwt). So far I had created a custom widget consisted of the plot area and a button "Plot it". They were both in the same class so I could connect the button signal to an internal function (getData(Qstring)) that would read some data .txt file and plot it to the plotting area.
Now I am trying to have a menubar with openFile so I can choose which data file I want my application to plot. I have managed to create the widgets (mainwindow,menubar, plotarea) but I haven't understood well how to connect the menubar (i.e. the data file i choose from open file menu) with the slot "getData(Qstring)" of the plotArea. I am definitely missing out the hierarchy of these widgets: mainwindow,menubar, plotarea
Any ideas?
Re: how to connect an openfile signal to a widget slot?
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.
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect ( ui->actionOpen, SIGNAL(triggered()), this, SLOT(handleFileOpenClick()));
connect ( ui->actionSave, SIGNAL(triggered()), this, SLOT(handleFileSaveClick()));
connect ( ui->actionNew, SIGNAL(triggered()), this, SLOT(handleNewClick()));
}
Code:
void MainWindow::handleFileOpenClick()
{
if ( settings->contains(_keyLastFilePath))
{
prevFilePath = settings->value(_keyLastFilePath).toString();
}
tr("Open SCXML Input File"), prevFilePath, tr("SCXML Files (*.scxml)"));
settings->setValue(_keyLastFilePath, fileName);
project = new SMProject(ui->graphicsView);
project->readInputFile(fileName);
}
void MainWindow::handleFileSaveClick()
{
if ( project == NULL) return;
if ( settings->contains(_keyLastFilePath))
{
prevFilePath = settings->value(_keyLastFilePath).toString();
}
tr("Open SCXML Input File"), prevFilePath, tr("SCXML Files (*.scxml)"));
settings->setValue(_keyLastFilePath, fileName);
project->save(fileName);
}
Re: how to connect an openfile signal to a widget slot?
thanks,
I am going to test these suggestions straight away.
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
Code:
error: ‘my2dPlot* MyWidget::mainPlot’ is private
when i try to add this line to the openfile action
Code:
widget->mainPlot->plotMainCurve(fileName);
-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")?
Re: how to connect an openfile signal to a widget slot?
before it was:
Code:
{
public:
private:
my2dPlot *mainPlot;
....
{
and now i moved the mainPlot member to the public:
Code:
{
public:
my2dPlot *mainPlot;
....
{
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?
Re: how to connect an openfile signal to a widget slot?
Quote:
Originally Posted by
fatecasino
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:
Code:
my2dPlot* Get2DPlot() {return my2dplot;} const;
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:
Code:
widget->mainPlot->plotMainCurve(fileName);
Re: how to connect an openfile signal to a widget slot?
What about
Code:
{
public:
public slots:
void plotFromFile( const QString& file ){
this->mainPlot->plotMainCurve(file);
}
private:
my2dPlot *mainPlot;
....
{
?
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??
Code:
this->mainPlot->plotMainCurve(file);
Code:
widget->mainPlot->plotMainCurve(fileName);
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:
Code:
widget->Get2DPlot()->plotMainCurve(file)
Re: how to connect an openfile signal to a widget slot?
Quote:
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
Quote:
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 )