QTableView does not detect doubleClick [duplicate] - c++

My application starts with an empty table, and I want to imlement different methods to add items. One should be by double-clicking the table's unused area (or "background") that is not occupied by any cells. When a cell is double-clicked, I want the default behavior.
I've found way to do this by re-implementing QAbstractScrollArea::mouseDoubleClickEvent() method in my TestTable class:
#include <QMouseEvent>
#include <QTableView>
class TestTable : public QTableView
{
Q_OBJECT
signals:
void backgroundDoubleClickEvent(void);
protected:
void mouseDoubleClickEvent (QMouseEvent* e)
{
if (indexAt(e->pos()).isValid())
{
QTableView::mouseDoubleClickEvent(e);
}
else
{
e->accept();
emit backgroundDoubleClickEvent();
}
}
};
This works, but is there a more elegant way of doing this without subclassing QTableView?
I'm not aware of any limitations of my current implementation. Are there obvious caveats?

If you don't want to subclass QTableView, try installEventFilter

Related

What signal emitted when hide a row or column in QTableView?

As QTableView has public function setRowHidden() and setColumnHidden(), but if person A performs hide operation, how can person B get to know the changes? If there is a signal available that gives an idea about the geometry changes in QTableView ?
Thanks ahead.
No, signal is not emitted an doc doesn't say anything about it, but you can make it easily by yourself.
Just create subclass of QTableView and override setRowHidden and setColumnHidden methods, call original methods and add your signal emissions inside. Something like that:
MyTableView.h
#include <QTableView>
class MyTableView : public QTableView
{
Q_OBJECT
public:
MyTableView(QWidget *parent = Q_NULLPTR);
~MyTableView();
void setColumnHidden(int column, bool hide);
void setRowHidden(int row, bool hide);
signals:
void columnHidden(int column, bool hide);
void rowHidden(int row, bool hide);
};
MyTableView.cpp
#include "mytableview.h"
MyTableView::MyTableView(QWidget *parent) : QTableView(parent)
{
}
MyTableView::~MyTableView()
{
}
void MyTableView::setRowHidden(int row, bool hide)
{
QTableView::setRowHidden(row, hide);
emit rowHidden(row, hide);
}
void MyTableView::setColumnHidden(int column, bool hide)
{
QTableView::setColumnHidden(column, hide);
emit columnHidden(column, hide);
}
Now you can call your overloaded methods just like the original ones.
With the happiest mood, I finally found the solution to observe the hide/show behavior of a row/column in tableView. The method still goes-back to the signal/slot mechanism. Just because of strange terminology in Qt's system, some concepts are really not very straight-forward and confusing.
QTableView *tableView = new QTableView;
tableView->setRowHidden(0, true);
This will make a tableView's the 0st row hide. This operation actually just makes the row's section resizes. The result of this is the size of the section is ZERO, and signal sectionResized() emitted.

How to know when the VerticalScrollBar is showing?

I need to know when the verticalScrollBar of my QTableWidget is being shown.
I am currently using the following instruction:
Header:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QWidget>
#include <QScrollBar>
namespace Ui {
class MyClass;
}
class MyClass: public QWidget
{
Q_OBJECT
public:
explicit MyClass(QWidget *parent = 0);
~MyClass();
private:
void populateTable(QVector<QString> content);
private:
Ui::MyClass *ui;
};
#endif // MYCLASS_H
Populate table function:
void MyClass::populateTable(QVector<QString> content)
{
while( ui->myTableWidget->rowCount() > 0 )
{
ui->myTableWidget->removeRow(0);
}
QTableWidgetItem* item;
for (int row = 0; row < content.length(); ++row)
{
ui->myTableWidget->insertRow(row);
item = new QTableWidgetItem( QString::number(row) );
item->setTextAlignment(Qt::AlignCenter);
ui->myTableWidget->setItem(row, 0, item);
item = new QTableWidgetItem( content.at(row) );
item->setTextAlignment(Qt::AlignCenter);
ui->myTableWidget->setItem(row, 1, item);
}
qDebug() << "This : " << this->isVisible();
qDebug() << "QTableWidget : " << ui->myTableWidget->isVisible();
qDebug() << "VerticalScrollBar : " << ui->myTableWidget->verticalScrollBar()->isVisible(); // <-HERE
}
Output:
// Called from the constructor
This : false
QTableWidget : false
VerticalScrollBar : false
// Called by a button pressed
This : true
QTableWidget : true
VerticalScrollBar : true
This : true
QTableWidget : true
VerticalScrollBar : false
But it returns a wrong value. When the ScrollBar is visible it returns false and when it is not visible it returns true. Note: myTableWidget (QTableWidget) is always visible.
Is there any other way that I can do this?
I'm using Qt version 5.3.2
In general the code you are using is supposed to work - checked on Qt 5.3.0.
However, you must be sure that when you are making the call the QTableWidget itself is visible.
For example if you make the call inside the MainWindow constructor you will certainly get a false answer. Only after the form is shown the call to isVisible() on particular scrollbar would return the correct value.
EDIT:
With your code pasted I was able to reproduce the issue. I needed to go through the Qt code a bit to see whats going on. Basically it turns out that for QTableView which is parent class of QTableWidget scroll bar values are updated via updateGeometries (do not confuse it with the regular updateGeometry the one I'm mentioning is protected). Internally this method is called either directly or the event is processed through the event loop. In short, it depends on whether you add columns or rows.
In your example, if you insertColumn instead of insertRow (and switch the arguments in setItem) after checking the visibility of horizontalScrollBar you will get the proper result right away.
I could confirm this by subclassing the QTableWidget and overriding event method. It shows that when adding columns following events are executed: MetaCall (invoke call) and LayoutRequest. On the other hand, when adding rows first event passed is Timer.
I'm not Qt implementer so I'm not sure what is the purpose the difference. However, this info helps solving your problem in a more elegant way.
You can implement MyTableWidget which overrides the event method.
class MyTableWidget: public QTableWidget
{
Q_OBJECT
public:
bool event(QEvent *e) override
{
const bool result = QTableWidget::event(e);
if (e->type() == QEvent::LayoutRequest)
{
// call what you need here
// or emit layoutUpdated(); and connect some slots which perform
// processing dependent on scrollbar visibility
}
return result;
}
signals:
void layoutUpdated();
}
However, such event might get called in other situations not only when the view needs to be updated due to model data updates.
Another solution would be to avoid overriding the event method but creating your own method to trigger the required updates. For example:
void MyTableWidget::updateLayout()
{
QEvent ev{QEvent::LayoutRequest};
QTableWidget::updateGeometries();
QTableWidget::event(&ev);
}
This would call directly updateGeometries which recalculates scrollbar min/max values and perform a direct event method call for LayoutRequest (without processing through eventloop). Which if I'm correct indirectly updates scrollbar visibility.
Calling this method before checking the visibility should also fix your problem.
ui->myTableWidget->updateLayout();
qDebug() << "VerticalScrollBar : " << ui->myTableWidget->verticalScrollBar()->isVisible();
// prints "VerticalScrollBar : true false"
I wanted to get notified via a signal, when a QScrollBar gets visible. I'm writing this answer here, since this is the first google result I got and no result had a proper answer.
First of all, there is no signal for changed visibility, so we will use valueChanged.
Now you can add a function that checks if the scrollbar isVisible() or not.
Problem: It won't work (and someone with more knowledge in Qt could probably explain why, I sadly cannot).
The trick is, to use a QTimer (Python code):
self.horizontalScrollBar().valueChanged.connect(lambda x: QTimer.singleShot(0, some_func))

Implementing a shift-click modifier to a specific form widget?

If I use Qt Designer for my application's main window -- and the application contains many different widgets inside of it -- how do I further customize those widgets sepcifically? For example, I have a QTableView widget inside of my main application. Upon debugging the application, a ui_myapplication.h file gets created from the Qt Designer .ui form. Suppose I wanted to add some extra things to the widgets defined in that file. How would I if it's created at runtime?
Example:
In myApplication.cpp, I have this block of code:
void myApplication::mousePressEvent(QMouseEvent* event) {
if(event->modifiers() & Qt::ShiftModifier) {
if(event->button() == Qt::LeftButton) {
qDebug() << "shift modifier";
ui->tableView->setSortingEnabled(false);
}
}
}
This is similar to what I want, but not exactly. The idea is to have the QTableView widget named tableView (which is contained within the main application that I created in Qt Designer) to disable the table's sorting functionality when I hold Shift and left-click a column header. (the end goal is to make it so that Shift+clicking will disable column sorting for a short while so I can select all items in the column instead of having it sort the column).
The code above will only work if I Shift+Click the very bottom of the main application (in the space where there are no other widgets). That makes sense. But how do I make it so that doing a Shift+click inside the tableView widget will trigger the qDebug() << "shift modifier"; line?
I'd want something similar to this: (pseudocode):
void myApplication::mousePressEvent(QMouseEvent* event) {
if(target == ui->tableView->horizontalheader() && event->modifiers() & Qt::ShiftModifier) {
if(event->button() == Qt::LeftButton) {
qDebug() << "shift modifier";
ui->tableView->setSortingEnabled(false);
}
}
}
How can I do this?
EDIT: research had led me to believe I can do this with an event filter that could target a specific widget. Am I on the right track?
EDIT 2: Thanks to goug's answer below, I was able to accomplish what I needed by subclassing QTableView and then promoting my existing form's QTableView to the new class. See below:
mytableview.h
#ifndef MYTABLEVIEW_H
#define MYTABLEVIEW_H
#include "mytableview.h"
#include <QTableView>
class MyTableView : public QTableView
{
Q_OBJECT
public:
explicit MyTableView(QWidget * parent = 0);
~MyTableView();
protected:
void mousePressEvent(QMouseEvent *event);
};
#endif // MYTABLEVIEW_H
mytableview.cpp
#include "mytableview.h"
#include <QDebug>
MyTableView::MyTableView(QWidget* parent)
{
}
MyTableView::~MyTableView()
{
}
void MyTableView::mousePressEvent(QMouseEvent* event) {
if(event->modifiers() & Qt::ShiftModifier) {
if(event->button() == Qt::LeftButton) {
qDebug() << "shift modifier";
setSortingEnabled(false);
}
}
}
There's a couple of different approaches you could use here. You could subclass QTableView, and then in Qt Designer, you place a QTableView as normal, but then promote it to your derived class. The generated code creates an instance of your class rather than QTableView. In Qt Designer, right-click on the table view and select from the Promote options. You'll have to enter your class details the first time. I'd be inclined to go this route especially if there's other custom behavior you want to implement on the table view.
Another option is to create a new class and install it as an event filter on your QTableView. Your new class then gets the events before they go to the QTableView and you can act accordingly on them. Look up installEventFilter in Qt Assistant, and that'll get you to the details of how to do this.

Catch double click in QTableView's unused area

My application starts with an empty table, and I want to imlement different methods to add items. One should be by double-clicking the table's unused area (or "background") that is not occupied by any cells. When a cell is double-clicked, I want the default behavior.
I've found way to do this by re-implementing QAbstractScrollArea::mouseDoubleClickEvent() method in my TestTable class:
#include <QMouseEvent>
#include <QTableView>
class TestTable : public QTableView
{
Q_OBJECT
signals:
void backgroundDoubleClickEvent(void);
protected:
void mouseDoubleClickEvent (QMouseEvent* e)
{
if (indexAt(e->pos()).isValid())
{
QTableView::mouseDoubleClickEvent(e);
}
else
{
e->accept();
emit backgroundDoubleClickEvent();
}
}
};
This works, but is there a more elegant way of doing this without subclassing QTableView?
I'm not aware of any limitations of my current implementation. Are there obvious caveats?
If you don't want to subclass QTableView, try installEventFilter

Get previous value of QComboBox, which is in a QTableWidget, when the value is changed

Say I have a QTableWidget and in each row there is a QComboBox and a QSpinBox. Consider that I store their values is a QMap<QString /*Combo box val*/,int /*spin box val*/> theMap;
When comboBoxes value or spin boxes value is being changed I want to update theMap. So I should know what was the former value of the combo box in order to replace with the new value of the comboBox and also take care of the value of the spin box.
How can I do this?
P.S. I have decided to create a slot that when you click on a table, it stores the current value of the combo box of that row. But this works only when you press on row caption. In other places (clicking on a combobox or on a spinbox) itemSelectionChanged() signal of QTableWidget does not work.
So in general my problem is to store the value of the combo box of selected row, and the I will get ComboBox or SpinBox change even and will process theMap easily.
How about creating your own, derived QComboBox class, something along the lines of:
class MyComboBox : public QComboBox
{
Q_OBJECT
private:
QString _oldText;
public:
MyComboBox(QWidget *parent=0) : QComboBox(parent), _oldText()
{
connect(this,SIGNAL(editTextChanged(const QString&)), this,
SLOT(myTextChangedSlot(const QString&)));
connect(this,SIGNAL(currentIndexChanged(const QString&)), this,
SLOT(myTextChangedSlot(const QString&)));
}
private slots:
myTextChangedSlot(const QString &newText)
{
emit myTextChangedSignal(_oldText, newText);
_oldText = newText;
}
signals:
myTextChangedSignal(const QString &oldText, const QString &newText);
};
And then just connect to myTextChangedSignal instead, which now additionally provides the old combo box text.
I hope that helps.
A bit late but I had the same problem and solved in this way:
class CComboBox : public QComboBox
{
Q_OBJECT
public:
CComboBox(QWidget *parent = 0) : QComboBox(parent) {}
QString GetPreviousText() { return m_PreviousText; }
protected:
void mousePressEvent(QMouseEvent *e)
{
m_PreviousText = this->currentText();
QComboBox::mousePressEvent(e);
}
private:
QString m_PreviousText;
};
My suggestion is to implement a model, which would help you make a clean separation between the data, and the UI editing the data. Your model would then get notified that a given model index (row and column) changed to the new data, and you could change whatever other data you needed to at that point.
I was just having a similar issue, but for me i needed the previous index for something very trivial so defining and implementing a whole class for it was unjustified.
So what I did instead was keep an argument called say 'previousIndex' and updated it's value only after I had done everything I needed with it