never_ever
never_ever

Reputation: 185

Qt - How to get real size of maximized window that I have pointer to?

I have a method that get pointer to widget where should be setted some other widgets. I have to resize the main window, where that widget is placed and then I should get the real size of that widget.

I’ve tried to do this like:

void MyClass::setWidgets(QList<QWidget*> list, QWidget *parentWidget)
{
    QWidget* mainWindow = parentWidget->window();
    mainWindow->showMaximized();

    int width = parentWidget->size().width();
    int height = parentWidget->size().height();
    /*... rest of method...*/
}

That method is call from other class. But I read that I should wait for resizeEvent. Could anyone explain me how I should do this or if there is any option to get that size differently?

Upvotes: 2

Views: 2144

Answers (1)

Silas Parker
Silas Parker

Reputation: 8147

If you want to get events for a different object, you can install an event filter using QObject::installEventFilter.

A simple example for ResizeEvent is:

filter.hpp

#ifndef FILTER_HPP
#define FILTER_HPP

#include <QtGui>

class ResizeFilter : public QObject
{
  Q_OBJECT
  public:
    ResizeFilter();
  protected:
    bool eventFilter(QObject *obj, QEvent *event);
};

#endif

filter.cpp

#include "filter.hpp"

ResizeFilter::ResizeFilter() : QObject() {}

bool ResizeFilter::eventFilter(QObject *obj, QEvent *event)
{
  if (event->type() == QEvent::Resize)
  {
    QResizeEvent* resizeEv = static_cast<QResizeEvent*>(event);
    qDebug() << resizeEv->size();
  }
  return QObject::eventFilter(obj, event);
}

main.cpp

#include <QtGui>
#include "filter.hpp"

int main(int argc, char** argv)
{
  QApplication app(argc, argv);

  ResizeFilter filter;

  QWidget window;
  window.installEventFilter(&filter);
  window.showMaximized();

  return app.exec();
}

filter.pro

TEMPLATE = app
HEADERS = filter.hpp
SOURCES = main.cpp filter.cpp

When testing this on my PC it gave the output:

QSize(840, 420) 
QSize(1280, 952)

Upvotes: 1

Related Questions