I need behaviour in QTableView for changing of column width not only with help of QHeaderView, but directly on QTableView area itself. It will be good if I can set cursor pointer between cells in QTableView area and change the column width with "click and drag" on QTableView area itself.
Here a partial answer, showing how to access column separators
/** sense mouse position */
bool FreezeTableWidget::eventFilter(QObject *, QEvent *event) {
if (event->type() == QEvent::MouseMove) {
QMouseEvent *m = static_cast<QMouseEvent*>(event);
QPoint p = m->pos();
QRect r = visualRect(indexAt(p));
if (abs(p.x() - r.x()) < 3) // || abs(p.x() - r.right()) < 3)
setCursor(Qt::SplitHCursor);
else
unsetCursor();
}
return false;
}
eventFilter is a virtual function, but you must register it to get it called. I added in the FreezeTableWidget constructor:
qApp->installEventFilter(this);
edit: about position check, the suggestion from evilruff also works (albeit the computational efficiency argument is irrilevant), but in general I think that you could try an alternative, more general approach: namely to use the horizontalHeader item as a surrogate handler, because there we already have all the required, working, logic.
If you like to experiment with (and learn about) these GUI details, try to shift the vertical mouse position, and give the event to the header... I think it could work, and will be simpler than tracking the state by yourself.
Actually I don't see anything wrong with such behaviour. Especially for read-only table views where it could be really convenient.
Fastest way to implement this to install event handler which will catch mouse events and issue resize commands. Obviously you will have to calculate manually if mouse is over/around column separator.
UPD: to get an idea if cursor is around vertical axes you can use following method:
/* Returns the x-coordinate in contents coordinates of the given column. */
int x = tableView->columnViewportPosition(int column);
mouse event will give you mouse position and then you can easily see if it matches any of column axes.
UPD2: I would even implement it in a bit more complicated way to handle mousemove together with header resize/scroll events within one eventHandler... to cache column positions in a sorted list, to optimise time spent in mousemove handler.. so, you will get binary search and distance check.. but its of course depends how heavy is interface in general..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With