How to put 2 Widgets in a MainWindow ?
Hi,
I want to put 2 widgets (a QwtPlot and a QTableWidget) on the same mainwindow with a QVBoxLayout, but I see nothing when I try with this code:
Code:
{
double *buffer=NULL;
plot=new Plot(100,250,buffer,buffer+1,p);
data = new Data(250,buffer,p);
layout->addWidget(plot);
layout->addWidget(data);
//Start of on_watcher
data->on_watcher();
}
And here's my construcors:
data .h:
Code:
Data
(int _periode,
double* &_voies,
QWidget*parent
=0);
data.cpp:
Code:
Data
::Data(int _periode,
double * &_voies,
QWidget *parent
):QTableWidget(parent
),periode
( _periode
)
plot.h:
Code:
Plot
( int duree,
int periode,
double * _time,
double * _voie,
QWidget * p
= NULL );
plot.cpp:
Code:
Plot
::Plot(int duree,
int periode,
double* _time,
double* _voie,
QWidget*p
): QwtPlot(p
)
Thanks for your help !
Re: How to put 2 Widgets in a MainWindow ?
Try this:
Code:
QVBoxLayout* layout
= new QVBoxLayout(this);
//so that the layout can re-parent your widgets with this (the window itself) not the parent of the window (witch is most likely 0)
Re: How to put 2 Widgets in a MainWindow ?
Ok, if I change my mainwindow.cpp to this:
Code:
{
double *buffer=NULL;
plot=new Plot(100,250,buffer,buffer+1,this);
data = new Data(250,buffer,this);
data->on_watcher();
}
I see my QTableWidget in the left corner, and no plot at all...
Re: How to put 2 Widgets in a MainWindow ?
In the code from last post you didn't add the widgets to layout.
Did you did that in your actual project? (You still need to do that)
Code:
{
double *buffer=NULL;
plot=new Plot(100,250,buffer,buffer+1,p);
data = new Data(250,buffer,p);
layout->addWidget(plot);
layout->addWidget(data);
//Start of on_watcher
data->on_watcher();
}
Re: How to put 2 Widgets in a MainWindow ?
oopps , I meant:
Code:
{
double *buffer=NULL;
plot=new Plot(100,250,buffer,buffer+1,this);
data = new Data(250,buffer,this);
layout->addWidget(plot);
layout->addWidget(data);
data->on_watcher();
}
Re: How to put 2 Widgets in a MainWindow ?
Are you sure this is what you want
Code:
plot=new Plot(100,250,buffer,buffer+1,this); // the buffer pointer is NULL and you are adding 1 to that???
Re: How to put 2 Widgets in a MainWindow ?
That's not the problem....buffer acts as a chart...everything works fine in there....when I do a setCentralWidget(plot) I see my plot and my program does what it's supposed to..
Altought, now, I want to add a QTableWidget, which is created and filled in my data class, and I don't know how to put both on my MainWindow
Re: How to put 2 Widgets in a MainWindow ?
Do you get some warnings about mainWindow allready have a layout?
Try to add a QWidget *test = new QWidget(0); make it the parent of the layout QVBoxLayout* layout = new QVBoxLayout(test); and setCentralWidget(test);
Re: How to put 2 Widgets in a MainWindow ?
For a start you need to set the QMainWindow's central widget... there can be only one. To get more than one widget into the main window central area you need to encapsulate them.
Code:
#include <QtGui>
public:
layout->addWidget(widget1);
layout->addWidget(widget2);
central->setLayout(layout);
setCentralWidget(central); // you were missing this
}
};
int main(int argc, char *argv[])
{
mainwindow m;
m.show();
return app.exec();
}
Re: How to put 2 Widgets in a MainWindow ?
ok, thanks !!
when I put this code:
Code:
{
double *buffer=NULL;
plot=new Plot(100,250,buffer,buffer+1,this);
data = new Data(250,buffer,this);
layout->addWidget(plot);
layout->addWidget(data);
central->setLayout(layout);
setCentralWidget(central);
data->on_watcher();
}
I have exaclty what I want, but suddently the program shuts down with a code -1073741819 and I still get the error about the QLayout that Zlatomir talked about:
Code:
Starting C:\Users\Genevieve\Desktop\DETECTION\debug\DETECTION.exe...
QLayout: Attempting to add
QLayout "" to mainwindow
"", which already has a layout
C:\Users\Genevieve\Desktop\DETECTION\debug\DETECTION.exe exited with code -1073741819
When I debbuged with Visual it says:
Unhandled exception at 0x004056bc in DETECTION.exe: 0xC0000005: Access violation reading location 0x00000004.
Re: How to put 2 Widgets in a MainWindow ?
Make line 11 read: and the warning should go away.
The access violation is probably related to the buffer pointer; using it while it is NULL, accessing the "buffer+1" memory location, or trying to use the storage for the buffer pointer itself after it goes out of scope.
Re: How to put 2 Widgets in a MainWindow ?
Ok, eveything works now!!!
just a last thing.. I want my plot to take 2/3 of the window andmy QTableWidget 1/3, is it possible to that with a QVBoxLayout ?
Thanks for your help !!!
Re: How to put 2 Widgets in a MainWindow ?
You can use something like this:
Code:
PlotPolicy.setVerticalStretch(1); //don't remember the exact "formula", try with 1
plot->setSizePolicy(PlotPolicy);
if that doesn't do the right proportions, try this:
Code:
PlotPolicy.setVerticalStretch(2); //don't remember the exact "formula", so play with this values
plot->setSizePolicy(PlotPolicy);
DataPolicy.setVerticalStretch(1);
plot->setSizePolicy(DataPolicy);
Re: How to put 2 Widgets in a MainWindow ?
Thanks Zlatomir !
I used your second method and it works well !!!
A last last question...is it possible to center my QTableWidget with that QVBoxLayout?
I want to do that because when I resize my window manually with a mouse, the QTableWidget stays align at the left.
Is there a similar function to that exists ?
I search the documentation, but couldn't find any easy way to do this