January 17, 2013

redstoneleo redstoneleo
Ant Farmer
127 posts

How to Fullscreen the child widget

 

How to Fullscreen the child widget
Un-subscribe from this thread

How to Fullscreen the child widget

The UI is as following
http://img.my.csdn.net/uploads/201212/06/1354779576_2592.jpg

What I want to do is that
When doubleclick the black widget ,it should become in full screen mode ,then if I doubleclick again ,it should return back .
So how to ?

showFullScreen()only affects windows

but when I setWindowFlags(Qt.Window) of that black widget ,the UI becomes like this

http://img.my.csdn.net/uploads/201212/06/1354779928_2090.jpg

so how can I implement this functionality ?

BTW ,Phonon.VideoWidget Inherits QWidget and it has setFullScreen() method ,who know how it is implemented ?
it would be better to give me some code example !

9 replies

January 17, 2013

redstoneleo redstoneleo
Ant Farmer
127 posts
the black widget is QWidget

January 17, 2013

Sam Sam
Ant Farmer
682 posts

The black widget lies inside the MainWindow of your application , so as per my understanding when you doubleClick on the black widget you want to maximize the application and if maximized then on double click you want to resize it back.

One option is to subclass QWidget( for your black Widget) and reimplement mouseDoubleClick() function.

Here is a test code :-

MyWidget.h

  1. class MyWidget : public QWidget
  2. {
  3.     Q_OBJECT
  4. public:
  5.     explicit MyWidget(QWidget *parent = 0);
  6.  
  7.  
  8. protected:
  9.     void mouseDoubleClickEvent(QMouseEvent *event);
  10.  
  11. signals:
  12.     void doubleClicked();
  13. public slots:
  14.     void onDoubleClicked();
  15.    
  16. };

MyWidget.cpp

  1. MyWidget::MyWidget(QWidget *parent) :
  2.     QWidget(parent)
  3. {
  4.     connect(this,SIGNAL(doubleClicked()),SLOT(onDoubleClicked()));
  5. }
  6.  
  7. void MyWidget::mouseDoubleClickEvent(QMouseEvent *event)
  8. {
  9.     emit doubleClicked();
  10. }
  11.  
  12.  
  13. void MyWidget::onDoubleClicked()
  14. {
  15.     if (parentWidget()->isMaximized())
  16.     {
  17.         // you can implement to move the mainwindow to the center of screen.
  18.         qDebug() << parentWidget(); //to see if parentWidget is mainwindow
  19.         this->parentWidget()->resize(300,300);
  20.  
  21.     } else
  22.     {
  23.         this->parentWidget()->showMaximized();
  24.     }
  25. }

Other option is that in your mainwindow you can override eventFilter() function and use installEventFilter() for your widget.

code:-

MainWindow.h

  1. class MainWindow : public QMainWindow
  2. {
  3.     Q_OBJECT
  4.    
  5. public:
  6.     explicit MainWindow(QWidget *parent = 0);
  7.     ~MainWindow();
  8.  
  9. public slots:
  10.         void onDoubleClicked();
  11.  
  12. signals:
  13.         void doubleClicked();
  14.  
  15. protected:
  16.     void mousePressEvent(QMouseEvent *event);
  17.     bool eventFilter(QObject *target, QEvent *event);
  18.    
  19. private:
  20.     Ui::MainWindow *ui;
  21.     QWidget *widget;
  22.  
  23.  
  24. };

MainWindow.cpp

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow)
  4. {
  5.     ui->setupUi(this);
  6.  
  7.     widget = new QWidget(this);
  8.     widget->installEventFilter(this);
  9.     widget->setAutoFillBackground(true);
  10.     widget->setPalette(QPalette(Qt::red));
  11.  
  12.     setCentralWidget(widget);
  13.  
  14.     connect(this,SIGNAL(doubleClicked()),SLOT(onDoubleClicked()));
  15.  
  16. }
  17.  
  18. MainWindow::~MainWindow()
  19. {
  20.     delete ui;
  21. }
  22.  
  23. void MainWindow::onDoubleClicked()
  24. {
  25.     if (isMaximized())
  26.     {
  27.         resize(300,300);
  28.  
  29.     } else
  30.     {
  31.         showMaximized();
  32.     }
  33. }
  34.  
  35. bool MainWindow::eventFilter(QObject *target, QEvent *event)
  36. {
  37.     if (target == widget)
  38.     {
  39.         if (event->type() == QEvent::MouseButtonDblClick)
  40.         {
  41.             QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  42.             if (mouseEvent->button() == Qt::LeftButton )
  43.             {
  44.                 emit doubleClicked();
  45.                 return QMainWindow::eventFilter(target,event);
  46.             }
  47.         }
  48.     }
  49.     return QMainWindow::eventFilter(target,event);
  50. }

January 17, 2013

redstoneleo redstoneleo
Ant Farmer
127 posts

Sam wrote:
The black widget lies inside the MainWindow of your application , so as per my understanding when you doubleClick on the black widget you want to maximize the application and if maximized then on double click you want to resize it back.

One option is to subclass QWidget( for your black Widget) and reimplement mouseDoubleClick() function.

Here is a test code :-

MyWidget.h

  1. class MyWidget : public QWidget
  2. {
  3.     Q_OBJECT
  4. public:
  5.     explicit MyWidget(QWidget *parent = 0);
  6.  
  7.  
  8. protected:
  9.     void mouseDoubleClickEvent(QMouseEvent *event);
  10.  
  11. signals:
  12.     void doubleClicked();
  13. public slots:
  14.     void onDoubleClicked();
  15.    
  16. };

MyWidget.cpp

  1. MyWidget::MyWidget(QWidget *parent) :
  2.     QWidget(parent)
  3. {
  4.     connect(this,SIGNAL(doubleClicked()),SLOT(onDoubleClicked()));
  5. }
  6.  
  7. void MyWidget::mouseDoubleClickEvent(QMouseEvent *event)
  8. {
  9.     emit doubleClicked();
  10. }
  11.  
  12.  
  13. void MyWidget::onDoubleClicked()
  14. {
  15.     if (parentWidget()->isMaximized())
  16.     {
  17.         // you can implement to move the mainwindow to the center of screen.
  18.         qDebug() << parentWidget(); //to see if parentWidget is mainwindow
  19.         this->parentWidget()->resize(300,300);
  20.  
  21.     } else
  22.     {
  23.         this->parentWidget()->showMaximized();
  24.     }
  25. }

Other option is that in your mainwindow you can override eventFilter() function and use installEventFilter() for your widget.

code:-

MainWindow.h

  1. class MainWindow : public QMainWindow
  2. {
  3.     Q_OBJECT
  4.    
  5. public:
  6.     explicit MainWindow(QWidget *parent = 0);
  7.     ~MainWindow();
  8.  
  9. public slots:
  10.         void onDoubleClicked();
  11.  
  12. signals:
  13.         void doubleClicked();
  14.  
  15. protected:
  16.     void mousePressEvent(QMouseEvent *event);
  17.     bool eventFilter(QObject *target, QEvent *event);
  18.    
  19. private:
  20.     Ui::MainWindow *ui;
  21.     QWidget *widget;
  22.  
  23.  
  24. };

MainWindow.cpp

@MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{ ui->setupUi(this);

widget = new QWidget(this); widget->installEventFilter(this); widget->setAutoFillBackground(true); widget->setPalette(QPalette(Qt::red)); setCentralWidget(widget); connect(this,SIGNAL),SLOT));

}

MainWindow::~MainWindow()
{ delete ui;
}

void MainWindow::onDoubleClicked()
{ if (isMaximized()) { resize(300,300);

} else { showMaximized(); } }

bool MainWindow::eventFilter(QObject *target, QEvent *event)
{ if (target widget) { if (event->type() QEvent::MouseButtonDblClick) { QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); if (mouseEvent->button() == Qt::LeftButton ) { emit doubleClicked(); return QMainWindow::eventFilter(target,event); } } } return QMainWindow::eventFilter(target,event);
}@

NOTE : When doubleclick the black widget ,it should become in full screen mode ,i.e. the full screen of my computer should become black (that is just like a player play video in full screen mode )then if I doubleclick again ,it should return back .

January 17, 2013

Sam Sam
Ant Farmer
682 posts

NOTE : When doubleclick the black widget ,it should become in full screen mode ,i.e. the full screen of my computer should become black (that is just like a player play video in full screen mode )then if I doubleclick again ,it should return back .

Thats more easy just set the following for your widget

  1. setWindowFlags(this->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
  2. setWindowState(this->windowState() | Qt::WindowFullScreen);

January 17, 2013

redstoneleo redstoneleo
Ant Farmer
127 posts

Sam wrote:
bq. NOTE : When doubleclick the black widget ,it should become in full screen mode ,i.e. the full screen of my computer should become black (that is just like a player play video in full screen mode )then if I doubleclick again ,it should return back .

Thats more easy just set the following for your widget

@setWindowFlags(this->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
setWindowState(this->windowState() | Qt::WindowFullScreen);@

Qt::WindowFullScreen only affects windows,but here the black widget is not window .

January 17, 2013

Sam Sam
Ant Farmer
682 posts

Qt::WindowFullScreen only affects windows,but here the black widget is not window

Are you sure ? coz that works for me , test code :-

  1. widget = new QWidget();
  2. widget->setAutoFillBackground(true);
  3. widget->setPalette(QPalette(Qt::red));
  4.  
  5. widget->setWindowFlags(widget->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
  6. widget->setWindowState(widget->windowState() | Qt::WindowFullScreen);
  7.  
  8. widget->show();

January 17, 2013

Sam Sam
Ant Farmer
682 posts

Okay here is the complete implementation

MainWindow.h

  1. class MainWindow : public QMainWindow
  2. {
  3.     Q_OBJECT
  4.    
  5. public:
  6.     explicit MainWindow(QWidget *parent = 0);
  7.     ~MainWindow();
  8.  
  9. public slots:
  10.     void onDoubleClicked();
  11.  
  12. signals:
  13.     void doubleClicked();
  14.  
  15. protected:
  16.     bool eventFilter(QObject *target, QEvent *event);
  17.    
  18. private:
  19.     Ui::MainWindow *ui;
  20.     QWidget *widget;
  21.     bool isMaximized;
  22. };

MainWindow.cpp

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow)
  4. {
  5.     ui->setupUi(this);
  6.  
  7.     isMaximized = false;
  8.     widget = new QWidget(this);
  9.     widget->installEventFilter(this);
  10.     widget->setAutoFillBackground(true);
  11.     widget->setPalette(QPalette(Qt::red));
  12.  
  13.     setCentralWidget(widget);
  14.     connect(this,SIGNAL(doubleClicked()),SLOT(onDoubleClicked()));
  15. }
  16.  
  17. MainWindow::~MainWindow()
  18. {
  19.     delete ui;
  20. }
  21.  
  22. void MainWindow::onDoubleClicked()
  23. {
  24.     if ((widget->windowState() == Qt::WindowFullScreen) && isMaximized)
  25.     {
  26.         qDebug("Parent");
  27.         widget->setParent(this);
  28.         setCentralWidget(widget);
  29.         isMaximized = false;
  30.  
  31.     } else
  32.     {
  33.         qDebug("Maximize");
  34.         isMaximized = true;
  35.         widget->setParent(NULL);
  36.         widget->setWindowFlags(widget->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
  37.         widget->setWindowState(widget->windowState() | Qt::WindowFullScreen);
  38.         widget->show();
  39.  
  40.     }
  41. }
  42.  
  43. bool MainWindow::eventFilter(QObject *target, QEvent *event)
  44. {
  45.     if (target == widget)
  46.     {
  47.         if (event->type() == QEvent::MouseButtonDblClick)
  48.         {
  49.             QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  50.             if (mouseEvent->button() == Qt::LeftButton )
  51.             {
  52.                 emit doubleClicked();
  53.                 return QMainWindow::eventFilter(target,event);
  54.             }
  55.         }
  56.     }
  57.     return QMainWindow::eventFilter(target,event);
  58. }

This works as required.

March 19, 2013

wartburgritter wartburgritt..
Lab Rat
1 posts
redstoneleo wrote:
http://img.my.csdn.net/uploads/201212/06/1354779576_2592.jpghttp://img.my.csdn.net/uploads/201212/06/1354779928_2090.jpg

Hi I’m new but interested in this thread. The access to the jpgs is “403 Forbidden” ??!!

bernd

March 19, 2013

Sam Sam
Ant Farmer
682 posts

@wartburgritt

Welcome to devnet,

Are you looking for a solution to the problem specified in the first post ?

For the above images you can have a look at image 1 [google.ae] and image 2 [google.ae]

 
  ‹‹ Updating QSqlRelationalTableModels      [solved] Qt5 under Ubuntu cannot find -lGL ››

You must log in to post a reply. Not a member yet? Register here!