PDA

View Full Version : how to connect an openfile signal to a widget slot?



fatecasino
13th December 2010, 13:47
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?

high_flyer
13th December 2010, 13:57
have a look at QAction

daviddrell
13th December 2010, 14:12
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.



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

settings = new QSettings(this);

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()));

}



void MainWindow::handleFileOpenClick()
{
QString prevFilePath=QDir::homePath();
QString fileName ;

if ( settings->contains(_keyLastFilePath))
{
prevFilePath = settings->value(_keyLastFilePath).toString();
}

fileName = QFileDialog::getOpenFileName(this,
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;

QString prevFilePath=QDir::homePath();
QString fileName ;

if ( settings->contains(_keyLastFilePath))
{
prevFilePath = settings->value(_keyLastFilePath).toString();
}

fileName = QFileDialog::getSaveFileName(this,
tr("Open SCXML Input File"), prevFilePath, tr("SCXML Files (*.scxml)"));

settings->setValue(_keyLastFilePath, fileName);

project->save(fileName);
}

fatecasino
13th December 2010, 14:26
thanks,
I am going to test these suggestions straight away.

fatecasino
13th December 2010, 17:38
Hi,

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


error: ‘my2dPlot* MyWidget::mainPlot’ is private

when i try to add this line to the openfile action


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")?

fatecasino
13th December 2010, 21:16
before it was:



class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
private:
my2dPlot *mainPlot;
....
{


and now i moved the mainPlot member to the public:


class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
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?

FelixB
14th December 2010, 09:40
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:


my2dPlot* Get2DPlot() {return my2dplot;} const;

fatecasino
14th December 2010, 13:27
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:



widget->mainPlot->plotMainCurve(fileName);

stampede
14th December 2010, 13:35
What about

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
public slots:
void plotFromFile( const QString& file ){
this->mainPlot->plotMainCurve(file);
}
private:
my2dPlot *mainPlot;
....
{
?

fatecasino
14th December 2010, 15:08
@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??

this->mainPlot->plotMainCurve(file);


widget->mainPlot->plotMainCurve(fileName);

FelixB
14th December 2010, 15:24
"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:

widget->Get2DPlot()->plotMainCurve(file)

stampede
14th December 2010, 15:35
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 (http://www.codersource.net/c/c-tutorials/c-tutorial-friend.aspx) 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-What-are-your-favourite-C-books )