PDA

View Full Version : How to put 2 Widgets in a MainWindow ?



gen_mass
29th June 2010, 23:52
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:




mainwindow::mainwindow(QWidget*p)
{

double *buffer=NULL;

plot=new Plot(100,250,buffer,buffer+1,p);
data = new Data(250,buffer,p);

QVBoxLayout* layout = new QVBoxLayout(p);

layout->addWidget(plot);
layout->addWidget(data);

//Start of on_watcher
data->on_watcher();

}


And here's my construcors:

data .h:

Data(int _periode,double* &_voies,QWidget*parent=0);

data.cpp:

Data::Data(int _periode,double * &_voies,QWidget *parent):QTableWidget(parent),periode( _periode )

plot.h:

Plot( int duree, int periode,double * _time, double * _voie, QWidget * p = NULL );

plot.cpp:

Plot::Plot(int duree,int periode,double* _time,double* _voie,QWidget*p): QwtPlot(p)

Thanks for your help !

Zlatomir
30th June 2010, 00:01
Try this:


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)

gen_mass
30th June 2010, 00:26
Ok, if I change my mainwindow.cpp to this:


mainwindow::mainwindow(QWidget*p)
{

double *buffer=NULL;

plot=new Plot(100,250,buffer,buffer+1,this);
data = new Data(250,buffer,this);

QVBoxLayout* layout = new QVBoxLayout(this);
data->on_watcher();
}


I see my QTableWidget in the left corner, and no plot at all...

Zlatomir
30th June 2010, 00:33
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)



mainwindow::mainwindow(QWidget*p)
{

double *buffer=NULL;

plot=new Plot(100,250,buffer,buffer+1,p);
data = new Data(250,buffer,p);

QVBoxLayout* layout = new QVBoxLayout(this);

layout->addWidget(plot);
layout->addWidget(data);

//Start of on_watcher
data->on_watcher();

}

gen_mass
30th June 2010, 00:35
oopps , I meant:


mainwindow::mainwindow(QWidget*p)
{

double *buffer=NULL;

plot=new Plot(100,250,buffer,buffer+1,this);
data = new Data(250,buffer,this);

QVBoxLayout* layout = new QVBoxLayout(this);

layout->addWidget(plot);
layout->addWidget(data);

data->on_watcher();

}

Zlatomir
30th June 2010, 00:40
Are you sure this is what you want


plot=new Plot(100,250,buffer,buffer+1,this); // the buffer pointer is NULL and you are adding 1 to that???

gen_mass
30th June 2010, 00:56
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

Zlatomir
30th June 2010, 01:16
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);

ChrisW67
30th June 2010, 01:28
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.



#include <QtGui>

class mainwindow: public QMainWindow {
public:
mainwindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QWidget(this); // a central widget
QLabel *widget1 = new QLabel("one widget", this);
widget1->setFrameStyle(QFrame::Box | QFrame::Plain);
QLabel *widget2 = new QLabel("two widget", this);
widget2->setFrameStyle(QFrame::Box | QFrame::Plain);

QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(widget1);
layout->addWidget(widget2);

central->setLayout(layout);
setCentralWidget(central); // you were missing this
}

};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

mainwindow m;
m.show();

return app.exec();
}

gen_mass
30th June 2010, 09:54
ok, thanks !!
when I put this code:


mainwindow::mainwindow(QWidget*p)
{

double *buffer=NULL;

plot=new Plot(100,250,buffer,buffer+1,this);
data = new Data(250,buffer,this);

QWidget *central = new QWidget(0); // a central widget.

QVBoxLayout *layout = new QVBoxLayout(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:


Starting C:\Users\Genevieve\Desktop\DETECTION\debug\DETECTI ON.exe...
QLayout: Attempting to add QLayout "" to mainwindow "", which already has a layout
C:\Users\Genevieve\Desktop\DETECTION\debug\DETECTI ON.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.

ChrisW67
30th June 2010, 10:14
Make line 11 read:
QVBoxLayout *layout = new QVBoxLayout(central); 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.

gen_mass
30th June 2010, 11:16
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 !!!

Zlatomir
30th June 2010, 11:46
You can use something like this:


QSizePolicy PlotPolicy = plot->sizePolicy();
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:


QSizePolicy PlotPolicy = plot->sizePolicy();
PlotPolicy.setVerticalStretch(2); //don't remember the exact "formula", so play with this values
plot->setSizePolicy(PlotPolicy);

QSizePolicy DataPolicy = data->sizePolicy();
DataPolicy.setVerticalStretch(1);
plot->setSizePolicy(DataPolicy);

gen_mass
1st July 2010, 13:01
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
QTableWidget::setAlignement(Qt::AlignCenter) that exists ?
I search the documentation, but couldn't find any easy way to do this