PDA

View Full Version : child widget resize to parent widget



bobFromAccounting
22nd December 2010, 01:15
Hello!

I am trying to force a subclass I made called Spreadsheet to automatically expand to the borders of its parent widget.

Here is appearing as it is, and the red arrows show what I am trying to achieve :

http://img98.imageshack.us/img98/1295/suckss.jpg

I did not post all the code, only the code that I thought was relevant.

spreadsheet.hpp


#ifndef SPREADSHEET_HPP
#define SPREADSHEET_HPP

#include <QTableWidget>

class Spreadsheet : public QTableWidget
{
Q_OBJECT

public:
Spreadsheet(int r, int c, QWidget *parent = 0);

private:
QTableWidget* table;
};

#endif



spreadsheet.cc


#include <QtGui>

#include "spreadsheet.hpp"

Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(parent)
{
table = new QTableWidget(r,c,this);
}



mainwindow.cc constructor


MainWindow::MainWindow()
{
QWidget* widget = new QWidget;
setCentralWidget(widget);

tabWidget = new QTabWidget;
sp = new Spreadsheet(200,5, tabWidget); // sp is of type Spreadsheet

tabWidget->addTab(sp, tr("First Tab"));

QHBoxLayout* layout = new QHBoxLayout;
layout->setMargin(3);
layout->addWidget(tabWidget);

widget->setLayout(layout);

createActions();
createMenus();

QString message = tr("Begin by opening a previous file ");
statusBar()->showMessage(message);

setWindowTitle(tr("Menus"));
}




How would I go about achieving of what is demonstrated with the arrows?

Thanks

tbscope
22nd December 2010, 08:01
A layout will work

bobFromAccounting
22nd December 2010, 12:31
For the main widget? There is already a layout.

I also had a layout for the spreadsheet inside the tab too; it did not work. I will try again nevertheless.

I'm just about ready to give up on Qt... I spent about 4-5 trying to figure out this one issue yesterday :(

HelderC
22nd December 2010, 12:39
Set the layout to parent widget... A "horizontally" layout maybe works.

bobFromAccounting
22nd December 2010, 12:50
I emphasized the parent widged and how I applied a layout to it with "!!!!". I already have a layout for the parent( a horizontal one exactly ):



MainWindow::MainWindow()

{
!!!!!!->>QWidget* widget = new QWidget;

!!!!!->>setCentralWidget(widget);


tabWidget = new QTabWidget;

sp = new Spreadsheet(200,5, tabWidget); // sp is of type Spreadsheet

tabWidget->addTab(sp, tr("First Tab"));

!!!!!!->>QHBoxLayout* layout = new QHBoxLayout;
layout->setMargin(3);

!!!!!!->>layout->addWidget(tabWidget);

!!!!!!->>widget->setLayout(layout);

createActions();
createMenus();

QString message = tr("Begin by opening a previous file ");
statusBar()->showMessage(message);


setWindowTitle(tr("Menus"));
}

HelderC
22nd December 2010, 12:58
Look this code:


tab = new QWidget();
tab->setObjectName(QString::fromUtf8("tab2"));

QHBoxLayout *horizontalLayout = new QHBoxLayout(tab);

horizontalLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));

textEdit = new codeArea(tab);

textEdit->setObjectName(QString::fromUtf8("textEdit"));

horizontalLayout->addWidget(textEdit);

ui->tabWidget->addTab(tab, "Code");

Here I have the tabWidget and created a tab called tab of type QWidget...

bobFromAccounting
22nd December 2010, 14:26
Thank you for working with me so far :)

I tried that(HelderC code) and still get the identical issue. I think I better understand the issue : if you go and change Spreadsheet to QTableWidget, everything is fine. All the spacing and formatting is correct. As soon as I change QTableWidget to Spreadsheet, the format stops working.

I guess it has something to do with the spreadsheet class. I mean, I derived it correctly, right? Something MUST be wrong with the Spreadsheet class but what?



class Spreadsheet : public QTableWidget
{

Q_OBJECT

public:
Spreadsheet(int r, int c, QWidget *parent = 0);

private:
QTableWidget* table;

};
Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(parent)
{
table = new QTableWidget(r,c,this);
}

I attached all my code.
EDIT:: Updated file, cleaner code now.

norobro
22nd December 2010, 15:06
Something MUST be wrong with the Spreadsheet class but what?

You're almost there! Here's a hint: your Spreadsheet class is a QTableWidget.

bobFromAccounting
22nd December 2010, 15:26
You're almost there! Here's a hint: your Spreadsheet class is a QTableWidget.

I thought spreadsheets were supposed to QTableWidgets. What is the issue, kind sir? :P

norobro
22nd December 2010, 15:33
Try this:
Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(parent)
{
//table = new QTableWidget(r,c,this);
setRowcount(r);
setColumnCount(c);
}
And comment out the pointer in spreadsheet.hpp

stefanadelbert
11th February 2011, 02:53
One better:



Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(r, c, parent)
{}