PDA

View Full Version : Put QForm in function for header



kode
16th December 2010, 11:48
Hi all,

I want ot create a function that will draw the header of my application. It will be some logo with text I suppose(didn't think it yet) and have to resize with the window.

I have created window with frame for header and tabwidget as body. Here is the whole function:


MainWindow::MainWindow(QWidget *parent) : QMainWindow( parent)
{
QMainWindow *MainWindow = this;

if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 400);

QWidget *centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));

// frame
QGridLayout *gridLayout = new QGridLayout(centralWidget);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(11, 11, 11, 11);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));

// QFrame *frame = new QFrame(centralWidget);
// frame->setObjectName(QString::fromUtf8("frame"));
// QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
// sizePolicy.setHorizontalStretch(0);
// sizePolicy.setVerticalStretch(0);
// sizePolicy.setHeightForWidth(frame->sizePolicy().hasHeightForWidth());
// frame->setSizePolicy(sizePolicy);
// frame->setMaximumSize(QSize(1280, 50));
// frame->setMinimumSize(QSize(300, 50));
// frame->setFrameShape(QFrame::StyledPanel);
// frame->setFrameShadow(QFrame::Raised);

QWidget *frame = FrameHeader(this);

gridLayout->addWidget(frame, 0, 0, 1, 1);
gridLayout->addWidget(MainWindow, 0, 0, 1, 1);

QTabWidget *tabWidget = new QTabWidget(centralWidget);
tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Preferred);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(tabWidget->sizePolicy().hasHeightForWidth());
tabWidget->setSizePolicy(sizePolicy1);
tabWidget->setMinimumSize(QSize(300, 200));
QWidget *tab_masterdata = new QWidget();
tab_masterdata->setObjectName(QString::fromUtf8("tab"));
tabWidget->addTab(tab_masterdata, QString());
QWidget *tab_2 = new QWidget();
tab_2->setObjectName(QString::fromUtf8("tab_2"));
tabWidget->addTab(tab_2, QString());

gridLayout->addWidget(tabWidget, 1, 0, 1, 1);

MainWindow->setCentralWidget(centralWidget);
QMenuBar *menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 400, 21));
MainWindow->setMenuBar(menuBar);
QToolBar *mainToolBar = new QToolBar(MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);

QStatusBar *statusBar = new QStatusBar(MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MainWindow->setStatusBar(statusBar);

//retranslateUi(MainWindow);
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));

QMetaObject::connectSlotsByName(MainWindow);

}


The commented block is the frame that I want to put in function, but didn't know how to return the QFrame from the function. I try to make the function of type QFrame but QtCreator complains that QFrame is not a type. Here is the function:


QWidget MainWindow::FrameHeader(QWidget *parent)
{

QWidget *centralWidget = this->centralWidget();

QFrame *frame = new QFrame(centralWidget);
frame->setObjectName(QString::fromUtf8("frame"));
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(frame->sizePolicy().hasHeightForWidth());
frame->setSizePolicy(sizePolicy);
frame->setMaximumSize(QSize(1280, 50));
frame->setMinimumSize(QSize(300, 50));
frame->setFrameShape(QFrame::StyledPanel);
frame->setFrameShadow(QFrame::Raised);

return centralWidget;
}

the problem is that I can bot include the function in the centralWidget's layout here


gridLayout->addWidget(frame, 0, 0, 1, 1);

The result should be

***the window***
***************
* the frame *
***************
* tabWidget *
***************

I hope this will be more clear form my explanation. Thank in advance!

hackerNovitiate
16th December 2010, 14:58
QtCreator complains that QFrame is not a type
#include <QFrame> should help.

By the way, I assume that you want gridLayout to be inside centralWidget? If so, then you should note that this code...



QGridLayout *gridLayout = new QGridLayout(centralWidget);

...only sets the relationship between centralWidget and gridLayout in the "Object Tree" (see http://doc.trolltech.com/latest/objecttrees.html); it doesn't DISPLAY gridLayout. You will also need to call setLayout().




The result should be

***the window***
***************
* the frame *
***************
* tabWidget *
***************

Check out QVboxLayout; it might be easier than QGridLayout.