class WidgetDetacher
: public QObject { Q_OBJECT
public:
public slots:
void detachWidget();
void attachWidget();
void showContextMenu
(const QPoint &pos
);
private:
int _tabPos;
};
_parentTabWidget = parent;
_subject = subject;
if (_subject != 0) {
_subject->setContextMenuPolicy(Qt::CustomContextMenu);
connect(_subject,
SIGNAL(customContextMenuRequested
(const QPoint &)),
this,
SLOT(showContextMenu
(const QPoint &)));
}
}
void WidgetDetacher::detachWidget() {
if (_subject != 0) {
if (_subject->parentWidget() != 0) {
_tabPos = _parentTabWidget->currentIndex();
_tabLabel = _parentTabWidget->tabText(_tabPos);
_parentTabWidget->removeTab(_tabPos);
_subject->setParent(0);
_subject->show();
}
}
}
void WidgetDetacher::attachWidget() {
if (_subject != 0 && _subject->parentWidget() == 0) {
disconnect
(_parentTabWidget,
SIGNAL(destroyed
(QObject*)), _subject,
SLOT(close
()));
_parentTabWidget->insertTab(_tabPos, _subject, _tabLabel);
_parentTabWidget->setCurrentIndex(_tabPos);
}
}
void WidgetDetacher
::showContextMenu(const QPoint &pos
) { if (_subject != 0) {
if (_subject->parentWidget() != 0) {
menu.addAction(tr("Detach"), this, SLOT(detachWidget()));
} else {
menu.addAction(tr("Attach"), this, SLOT(attachWidget()));
}
menu.
exec(((QWidget*) _subject
)->mapToGlobal
(pos
));
}
}
class WidgetDetacher : public QObject {
Q_OBJECT
public:
WidgetDetacher(QTabWidget* parent, QWidget* subject);
public slots:
void detachWidget();
void attachWidget();
void showContextMenu(const QPoint &pos);
private:
QWidget* _subject;
QTabWidget* _parentTabWidget;
QString _tabLabel;
int _tabPos;
};
WidgetDetacher::WidgetDetacher(QTabWidget* parent, QWidget* subject) :
QObject(parent) {
_parentTabWidget = parent;
_subject = subject;
if (_subject != 0) {
_subject->setContextMenuPolicy(Qt::CustomContextMenu);
connect(_subject, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(showContextMenu(const QPoint &)));
}
}
void WidgetDetacher::detachWidget() {
if (_subject != 0) {
if (_subject->parentWidget() != 0) {
_tabPos = _parentTabWidget->currentIndex();
_tabLabel = _parentTabWidget->tabText(_tabPos);
_parentTabWidget->removeTab(_tabPos);
_subject->setParent(0);
_subject->show();
}
}
}
void WidgetDetacher::attachWidget() {
if (_subject != 0 && _subject->parentWidget() == 0) {
disconnect(_parentTabWidget, SIGNAL(destroyed(QObject*)), _subject, SLOT(close()));
_parentTabWidget->insertTab(_tabPos, _subject, _tabLabel);
_parentTabWidget->setCurrentIndex(_tabPos);
}
}
void WidgetDetacher::showContextMenu(const QPoint &pos) {
QMenu menu;
if (_subject != 0) {
if (_subject->parentWidget() != 0) {
menu.addAction(tr("Detach"), this, SLOT(detachWidget()));
} else {
menu.addAction(tr("Attach"), this, SLOT(attachWidget()));
}
menu.exec(((QWidget*) _subject)->mapToGlobal(pos));
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks