Results 1 to 2 of 2

Thread: QBoxLayout with QMainWindow vs QWidget

  1. #1
    Join Date
    Apr 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default QBoxLayout with QMainWindow vs QWidget

    Hello.I'd like to ask about the thing that is happening ,when I use QHBoxLayout with QMainWindow and with QWidget.I'm trying to change window title style, by connecting 2 widgets.
    This what's happening: Under QWidget
    1.jpg
    Here's the code for class under QWidget:
    TitleBar.h
    Qt Code:
    1. #ifndef MYTITLEBAR_H
    2. #define MYTITLEBAR_H
    3.  
    4. #include <QMainWindow>
    5. #include <QMouseEvent>
    6. #include <QToolButton>
    7. #include <QStyle>
    8. #include <QLabel>
    9. #include <QHBoxLayout>
    10. #include <QPixmap>
    11.  
    12. class MyTitleBar : public QWidget
    13. {
    14. public:
    15. MyTitleBar( QWidget* parent );
    16. public slots:
    17. void showSmall();
    18. void showMaxRestore();
    19.  
    20. protected:
    21. void mousePressEvent( QMouseEvent* me );
    22. void mouseMoveEvent( QMouseEvent* me );
    23.  
    24. private:
    25. QToolButton* minimize;
    26. QToolButton* maximize;
    27. QToolButton* close;
    28. QToolButton* custom;
    29. QPixmap restorePix, maxPix, closePix, minPix, customPix;
    30. bool maxNormal;
    31. QPoint startPos;
    32. QPoint clickPos;
    33. };
    34.  
    35. #endif // MYTITLEBAR_H
    To copy to clipboard, switch view to plain text mode 
    TitleBar.cpp
    Qt Code:
    1. #include "mytitlebar.h"
    2.  
    3. MyTitleBar::MyTitleBar( QWidget* parent )
    4. {
    5. this->setWindowFlags( Qt::FramelessWindowHint );
    6.  
    7. minimize = new QToolButton(this);
    8. maximize = new QToolButton(this);
    9. close = new QToolButton(this);
    10. custom = new QToolButton(this);
    11.  
    12. closePix.load( "close.png" );
    13. close->setIcon( closePix );
    14.  
    15. maxPix.load( "maximize.png" );
    16. maximize->setIcon( maxPix );
    17.  
    18. minPix.load( "minimize.png" );
    19. minimize->setIcon( minPix );
    20.  
    21. customPix.load( "custom_icon.png" );
    22. custom->setIcon( customPix );
    23.  
    24. QLabel* label = new QLabel(this);
    25. label->setText( "Custom Window" );
    26.  
    27. QHBoxLayout* HBox = new QHBoxLayout( this );
    28.  
    29. HBox->addWidget( custom );
    30. HBox->addWidget( label );
    31. HBox->addWidget( minimize );
    32. HBox->addWidget( maximize );
    33. HBox->addWidget( close );
    34.  
    35.  
    36.  
    37. }
    38.  
    39. void MyTitleBar::showSmall()
    40. {
    41.  
    42. }
    43.  
    44. void MyTitleBar::showMaxRestore()
    45. {
    46.  
    47. }
    48.  
    49. void MyTitleBar::mousePressEvent(QMouseEvent *me)
    50. {
    51.  
    52. }
    53.  
    54. void MyTitleBar::mouseMoveEvent(QMouseEvent *me)
    55. {
    56.  
    57. }
    To copy to clipboard, switch view to plain text mode 
    But under QMainWindow, happens something like this:
    2.jpg

    The code is exactly the same as for QWidget, but I've changed the parent and inheritance to QMainWindow.And returning to the question why is this happening and how I can fix this? In the main window it's just intancing the class and move it to the right place, that it'd look like a window title bar and is the same as for QWidget and QMainWindow. I need to use QMainWindow as parent, because the main window is inheritance of QMainWindow.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QBoxLayout with QMainWindow vs QWidget

    QMainWindow already has a layout.

    The content area of a main window is called the central widget, see QMainWindow::setCentralWidget().

    So if your code works in a widget, keep it there and set this widget as the central widget or add it to the actual central widget
    Or use a QToolBar and add it to the main window.

    Cheers,
    _

Similar Threads

  1. how to resize Qmainwindow with Qwidget ?
    By zeynepb.bil in forum Qt Programming
    Replies: 10
    Last Post: 28th September 2017, 23:48
  2. Qmainwindow QWidget size
    By seltra in forum Newbie
    Replies: 1
    Last Post: 3rd October 2010, 18:46
  3. QWidget on QMainWindow
    By jayreddy in forum Qt Programming
    Replies: 4
    Last Post: 6th January 2010, 08:27
  4. Resize QWidget in QMainWindow
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2007, 12:16
  5. QBoxLayout removeWidget problem
    By jaime in forum Qt Programming
    Replies: 3
    Last Post: 30th August 2006, 20:57

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.