2

I'm new to Qt and I don't know how to fix this. I have a QDockWidget that is put in some position when the user clicks the undock button (I don't know it's name). When this happens the widget visibility changes twice because it sort of disappears and appears again but undocked. To put it in some position after it gets undocked I use this in the visibility changed event:

if (ui->searchBar->isFloating()) {
    int x = (this->x() + this->width()) - 410;
    int y = (this->y() + this->height()) - 70;
    ui->searchBar->setGeometry(QRect(x,y,400,60));
}

The problem is that in Linux if I undock the widget by dragging it in stead of clicking the undock button, when I drop it in some place of the window it automatically moves to the position specified by the code above. It makes sense, however this doesn't happens in Windows(and I need the application to run in both OS). In Windows if you click the button it goes to the specified location but if you drag it then it stays in the position where you drop it.

To fix it I plan to use the above code only if the widget was not dragged but I'm not sure how to do this. Can somebody help me out or have a better idea on how to fix this?

Topo
  • 4,783
  • 9
  • 48
  • 70

1 Answers1

5

QDockWidget has undocumented button named 'qt_dockwidget_floatbutton'. You can access it like this:

QAbstractButton* button = 
        YourDockWidget->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");

connect(button, SIGNAL(clicked()), this, SLOT(yourSlot()));

So you can put your code above to that slot to move your widget only if it was undocked via the button.

hank
  • 9,553
  • 3
  • 35
  • 50
  • Thanks a lot, how did you get to know this if it is undocumented? – Topo Jun 26 '12 at 05:34
  • I read it [here](http://stackoverflow.com/questions/9559666/qdockwidget-causes-qt-to-crash) some days ago :) – hank Jun 26 '12 at 05:55