PDA

View Full Version : QTabWidget Corner Widget Problem



isamert
11th February 2011, 19:17
Hi,
I have qtabwidget with corner widget. I added QToolButton as corner widget.



btnMenu = new QToolButton(this);
btnMenu->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
btnMenu->setAutoRaise(true);
btnMenu->setIcon(QIcon::fromTheme("tab-new"));
btnMenu->setText(trUtf8("Pars"));
btnMenu->setShortcut(QKeySequence::AddTab);
btnMenu->setMenu(menu);
btnMenu->setPopupMode(QToolButton::MenuButtonPopup);
setCornerWidget(btnMenu,Qt::TopLeftCorner);


And I have "mouseRelaseEvent" for tabwidget like this:




void PTabWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
{
int index = tabBar()->tabAt(event->pos());
if (index != -1)
{
this->removeTab(index);
qDebug() << QString::number(index);
return;
}
else
{
return;
}
}

PTabWidget::mouseReleaseEvent(event);
}


When I click with mid-button to tabbar, the output is "The program has unexpectedly finished.". But when i remove the tool button, it works fine. Is it bug or I miss something ??

Sorry for my poor English.
Best regards.

wysota
11th February 2011, 19:24
One thing that's certainly invalid is that the event position is calculated relative to the tab widget and you are calling QTabBar::tabAt() which expects a position relative to the tab bar. I understand that you wish to remove a tab when you mid-click on it. If so, convert the coordinates to tab-bar's space and use deleteLater() instead of removeTab().

isamert
11th February 2011, 19:32
Yes, I want to remove a tab with mid-click. But how can I convert the coordinates to tab-bar's space? What do you mean with "tab-bar's space" ? I dont understand exactly.
Thanks for your reply.

wysota
11th February 2011, 19:51
See QWidget::mapTo family of methods.

isamert
11th February 2011, 20:06
I found diffirent way. I calculate point with buttons width. (My English is not enough for that :) ).



QPoint npoint;
npoint.setX(event->pos().x() - btnMenu->width());
npoint.setY(event->pos().y());
int index = tabBar()->tabAt(npoint);

New code:


void PTabWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
{
QPoint npoint;
npoint.setX(event->pos().x() - btnMenu->width());
npoint.setY(event->pos().y());
int index = tabBar()->tabAt(npoint);
if (index != -1)
{
this->widget(index)->deleteLater();
qDebug() << QString::number(index);
return;
}
else
{
return;
}
}

PTabWidget::mouseReleaseEvent(event);
}

wysota
12th February 2011, 00:10
That's not a good approach, you are not taking margins and spacing into consideration. It will fail if you click near a border of a tab. Use QWidget::mapTo() like this:

QPoint tabBarPoint = mapTo(tabBar()), event->pos());
int index = tabBar()->tabAt(tabBarPoint);

d_stranz
12th February 2011, 16:46
Maybe I am not seeing this correctly, but no matter what you do with the middle mouse button, doesn't this code set up an infinite loop if you click with anything except a middle button?



void PTabWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
{
// ...
return;
}

PTabWidget::mouseReleaseEvent(event); // <<<<
}


Maybe the OP is lucky and the tab widget handles the click with mousePressEvent so the code never handles that case.