Results 1 to 2 of 2

Thread: Put QForm in function for header

  1. #1
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1

    Default Put QForm in function for header

    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:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow( parent)
    2. {
    3. QMainWindow *MainWindow = this;
    4.  
    5. if (MainWindow->objectName().isEmpty())
    6. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    7. MainWindow->resize(800, 400);
    8.  
    9. QWidget *centralWidget = new QWidget(MainWindow);
    10. centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    11.  
    12. // frame
    13. QGridLayout *gridLayout = new QGridLayout(centralWidget);
    14. gridLayout->setSpacing(6);
    15. gridLayout->setContentsMargins(11, 11, 11, 11);
    16. gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    17.  
    18. // QFrame *frame = new QFrame(centralWidget);
    19. // frame->setObjectName(QString::fromUtf8("frame"));
    20. // QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    21. // sizePolicy.setHorizontalStretch(0);
    22. // sizePolicy.setVerticalStretch(0);
    23. // sizePolicy.setHeightForWidth(frame->sizePolicy().hasHeightForWidth());
    24. // frame->setSizePolicy(sizePolicy);
    25. // frame->setMaximumSize(QSize(1280, 50));
    26. // frame->setMinimumSize(QSize(300, 50));
    27. // frame->setFrameShape(QFrame::StyledPanel);
    28. // frame->setFrameShadow(QFrame::Raised);
    29.  
    30. QWidget *frame = FrameHeader(this);
    31.  
    32. gridLayout->addWidget(frame, 0, 0, 1, 1);
    33. gridLayout->addWidget(MainWindow, 0, 0, 1, 1);
    34.  
    35. QTabWidget *tabWidget = new QTabWidget(centralWidget);
    36. tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
    37. QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Preferred);
    38. sizePolicy1.setHorizontalStretch(0);
    39. sizePolicy1.setVerticalStretch(0);
    40. sizePolicy1.setHeightForWidth(tabWidget->sizePolicy().hasHeightForWidth());
    41. tabWidget->setSizePolicy(sizePolicy1);
    42. tabWidget->setMinimumSize(QSize(300, 200));
    43. QWidget *tab_masterdata = new QWidget();
    44. tab_masterdata->setObjectName(QString::fromUtf8("tab"));
    45. tabWidget->addTab(tab_masterdata, QString());
    46. QWidget *tab_2 = new QWidget();
    47. tab_2->setObjectName(QString::fromUtf8("tab_2"));
    48. tabWidget->addTab(tab_2, QString());
    49.  
    50. gridLayout->addWidget(tabWidget, 1, 0, 1, 1);
    51.  
    52. MainWindow->setCentralWidget(centralWidget);
    53. QMenuBar *menuBar = new QMenuBar(MainWindow);
    54. menuBar->setObjectName(QString::fromUtf8("menuBar"));
    55. menuBar->setGeometry(QRect(0, 0, 400, 21));
    56. MainWindow->setMenuBar(menuBar);
    57. QToolBar *mainToolBar = new QToolBar(MainWindow);
    58. mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
    59. MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
    60.  
    61. QStatusBar *statusBar = new QStatusBar(MainWindow);
    62. statusBar->setObjectName(QString::fromUtf8("statusBar"));
    63. MainWindow->setStatusBar(statusBar);
    64.  
    65. //retranslateUi(MainWindow);
    66. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    67.  
    68. QMetaObject::connectSlotsByName(MainWindow);
    69.  
    70. }
    To copy to clipboard, switch view to plain text mode 


    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:

    Qt Code:
    1. QWidget MainWindow::FrameHeader(QWidget *parent)
    2. {
    3.  
    4. QWidget *centralWidget = this->centralWidget();
    5.  
    6. QFrame *frame = new QFrame(centralWidget);
    7. frame->setObjectName(QString::fromUtf8("frame"));
    8. QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    9. sizePolicy.setHorizontalStretch(0);
    10. sizePolicy.setVerticalStretch(0);
    11. sizePolicy.setHeightForWidth(frame->sizePolicy().hasHeightForWidth());
    12. frame->setSizePolicy(sizePolicy);
    13. frame->setMaximumSize(QSize(1280, 50));
    14. frame->setMinimumSize(QSize(300, 50));
    15. frame->setFrameShape(QFrame::StyledPanel);
    16. frame->setFrameShadow(QFrame::Raised);
    17.  
    18. return centralWidget;
    19. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. gridLayout->addWidget(frame, 0, 0, 1, 1);
    To copy to clipboard, switch view to plain text mode 

    The result should be

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

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

  2. #2
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: Put QForm in function for header

    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...
    Qt Code:
    1. QGridLayout *gridLayout = new QGridLayout(centralWidget);
    To copy to clipboard, switch view to plain text mode 
    ...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.

  3. The following user says thank you to hackerNovitiate for this useful post:

    kode (17th December 2010)

Similar Threads

  1. Qt Creator How to auto-create function blocks from header declarations?
    By Asperamanca in forum Qt Tools
    Replies: 1
    Last Post: 29th October 2010, 10:21
  2. Replies: 3
    Last Post: 25th May 2010, 10:46
  3. Replies: 0
    Last Post: 10th March 2010, 09:13
  4. QStandardItem's header item and header label
    By feverzsj in forum Newbie
    Replies: 1
    Last Post: 14th January 2010, 20:57
  5. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 16:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.