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.