Get the position of a drop relative to an item on a QTreeWidget - qt

I have a custom QTreeWidget class with the dropEvent() method overridden.
Here is the method:
void QCustomTreeWidget::dropEvent(QDropEvent * event)
{
QModelIndex droppedIndex = indexAt(event->pos());
if (!droppedIndex.isValid())
return;
// other logic
QTreeWidget::dropEvent(event);
}
How can I determine if the item will be inserted above, inside or below the item on which it is dropped?

You need to use the DropIndicatorPosition. With a switch statement, you can easily achieve what you want.
bool bAbove = false; // boolean for the case when you are above an item
QModelIndex dropIndex = indexAt(event->pos());
DropIndicatorPosition dropIndicator = dropIndicatorPosition();
if (!dropIndex.parent().isValid() && dropIndex.row() != -1)
{
switch (dropIndicator)
{
case QAbstractItemView::AboveItem:
// manage a boolean for the case when you are above an item
bAbove = true;
break;
case QAbstractItemView::BelowItem:
// something when being below an item
break;
case QAbstractItemView::OnItem:
// you're on an item, maybe add the current one as a child
break;
case QAbstractItemView::OnViewport:
// you are not on your tree
break;
}
if(bAbove) // you are above an item
{
// manage this case
}
}

Related

How to pin a tab in Qt

Is it possible to pin a tab with Qt?
I want a tab to always stay in place (index 0) while still able to move other tabs.
So far I tried to listen to QTabBar::tabMoved and revert the move but that's too late. I don't want it even to attempt to move.
Worst case for me would be to be forced to change the mouse handling. Let me know please if there is an other way.
I have never found a nice way to do that. But, I used the fact that you can store raw data in the QTabBar to pin the tabs and undo a move if it was a pinned tab. It's not perfect and I still have some ugly behavior, but I didn't want to use mouse events, neither.
First, create a struct to store the current state of a tab:
struct PinnedTab
{
bool isPinned;
int currentIndex;
};
Q_DECLARE_METATYPE(PinnedTab); // For QVariant
Then, create a custom QTabBar to handle the move and use QTabWidget to replace the current tab bar (you have to do that before inserting the tabs):
class Bar: public QTabBar
{
public:
void pin(int const index)
{
PinnedTab info;
info.isPinned = true;
info.currentIndex = index; // TODO: move the tab to the left and do not use current index
setTabData(index, QVariant::fromValue(info));
}
Bar(QWidget* parent=nullptr): QTabBar(parent)
{}
virtual void tabLayoutChange() override
{
for (int i = 0; i != count(); ++i) // Check if a pinned tab has moved
{
if (tabData(i).isValid())
{
PinnedTab const info = tabData(i).value<PinnedTab>();
if (info.isPinned == true && i != info.currentIndex) {
rollbackLayout();
return;
}
}
}
for (int i = 0; i != count(); ++i)
{
if (tabData(i).isValid())
{
PinnedTab info = tabData(i).value<PinnedTab>();
info.currentIndex = i;
setTabData(i, QVariant::fromValue(info));
}
else
{
PinnedTab info;
info.isPinned = false;
info.currentIndex = i;
setTabData(i, QVariant::fromValue(info));
}
}
}
void rollbackLayout() {
for (int i = 0; i != count(); ++i)
{
if (tabData(i).isValid())
{
PinnedTab const info = tabData(i).value<PinnedTab>();
if (i != info.currentIndex) {
moveTab(i, info.currentIndex);
}
}
}
}
};
tabLayoutChange is called when the layout has changed. So, it will be called when you move a tab.
the rollbackLayout method is used to move each tab to the last position stored in the tab data.
Call pin to pin a tab with the given index.
I simplified my code for more clarity and you may have to redefine some behavior (for now, if you pin a tab, it will keep its current position and it will not handle the insert/remove tabs).

SelectionMode.MULTIPLE with TAB to navigate to next cell

I'm currently using TAB to navigate to next cell. selectNext() or selectRightCell() works fine when I'm using SelectionMode.SINGLE.
However, when using SelectionMode.MULTIPLE, its selecting multiple cells as I TAB.
I'm using a TableView. I need SelectionMode.MULTIPLE for the copy & paste function.
Is there a way to make it work in SelectionMode.MULTIPLE?
fixedTable.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
switch (event.getCode()){
case TAB:
if (event.isShiftDown()) {
fixedTable.getSelectionModel().selectPrevious();
} else {
fixedTable.getSelectionModel().selectNext();
}
event.consume();
break;
case ENTER:
return;
case C:
if(event.isControlDown()){
copySelectionToClipboard(fixedTable) ;
}
event.consume();
break;
case V:
if(event.isControlDown()){
pasteFromClipboard(fixedTable);
}
event.consume();
break;
default:
if (fixedTable.getEditingCell() == null) {
if (event.getCode().isLetterKey() || event.getCode().isDigitKey()) {
TablePosition focusedCellPosition = fixedTable.getFocusModel().getFocusedCell();
fixedTable.edit(focusedCellPosition.getRow(), focusedCellPosition.getTableColumn());
}
}
break;
}
}
});
You will need to handle the selection on your own. The reason is because the methods selectPrevious() and selectNext() tries to select the previous ( or the next ) without removing the current selected row ( when you set the selection mode to be SelectionMode.MULTIPLE) , also you can't use them and just remove the previous selecting by just calling clearSelection() because this will set the selected index to -1 and then the methods selectPrevious() and selectNext() will select the last or the first row only.
Here is how you could implement the selection on your own :
// the rest of your switch statement
...
case TAB:
// Find the current selected row
int currentSelection = table.getSelectionModel().getSelectedIndex();
// remove the previous selection
table.getSelectionModel().clearSelection();
if (event.isShiftDown()) {
currentSelection--;
} else {
currentSelection++;
}
// find the size of our table
int size = table.getItems().size() - 1;
// we was on the first element and we try to go back
if(currentSelection < 0){
// either do nothing or select the last entry
table.getSelectionModel().select(size);
}else if(currentSelection > size) {
// we are at the last index, do nothing or go to 0
table.getSelectionModel().select(0);
}else {
// we are between (0,size)
table.getSelectionModel().select(currentSelection);
}
event.consume();
break;
...

Fixed/freezed row in tableview

I have a TableView that displays the information contained in a QSortFilterProxyModel (whose model is a QAbstractTableModel)
Is there any way to fix/freeze in the TableView's top all rows with a specific property set to true, while maintaining filter and sorting for the other rows?
To put a specific row always on the tableview's top, I added the following logic to the QSortFilterProxyModel's lessThan method:
// Get left and right row
int leftRow = left.row();
int rightRow = right.row();
// Get specific data from left row
QModelIndex leftIndex = sourceModel()->index(leftRow, SPECIFIC_DATA_INDEX);
QString leftProperty = sourceModel()->data(leftIndex).toString();
// Get specific data from right row
QModelIndex rightIndex = sourceModel()->index(rightRow, SPECIFIC_DATA_INDEX);
QString rightProperty = sourceModel()->data(rightIndex).toString();
if(leftProperty .compare("Invalid") == 0) // put left on top if it has a specific property
if(sortOrder() == Qt::AscendingOrder) {
return true;
} else {
return false;
}
} else if(rightProperty .compare("Invalid") == 0) { // put right on top if it has a specific property
if(sortOrder() == Qt::AscendingOrder) {
return false;
} else {
return true;
}
}

Specializing a QAbstractProxyModel for adding a column: the table cells becomes empty

I have created a mixin-like proxy model (Qt5) which just adds an extra first column to another proxy model, for adding a QToolBar of actions to each row of the table view (for example, a "delete" button). The model just provides a way of populating a QList<QVariant> for the first column. The delegate must know what is the meaning of each QVariant (usually ints/enums identifying actions), and populate the QToolBar accordingly. As last feature, if there's no actions, no extra column is added (it behaves like a QIdentityProxyModel in that case). Once added, actions cannot be removed. That's a feature for another day.
The problem of today is that, when I insert actions (which I do before setting the model to the view), the cells are all blanks. So, I'm doing something wrong with the signals or who knows with what (I think the mistake is in the add_action function, at the end of the snippet):
template<class proxy_model>
class action_model : public proxy_model
{
QList<QVariant> l_actions;
public:
using base_t = proxy_model;
using base_t::base_t; // Inheriting constructors.
QModelIndex mapFromSource(const QModelIndex& source_idx) const override
{
if (!l_actions.empty() and source_idx.isValid())
return this->createIndex(source_idx.row(),
source_idx.column() + 1);
else // identity proxy case
return base_t::mapFromSource(source_idx);
} // same for mapToSource but with - 1 instead of + 1.
int columnCount(const QModelIndex& parent = QModelIndex()) const override
{ return this->base_t::columnCount() + !l_actions.empty(); }
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if (!l_actions.empty()) {
if (orientation == Qt::Horizontal and section == 0
and role == Qt::DisplayRole)
return "Actions"; // Testing.
else
return base_t::headerData(section - 1, orientation, role);
} else // identity proxy case
return base_t::headerData(section, orientation, role);
}
QVariant data(const QModelIndex& idx, int role) const override
{
if (!l_actions.empty()) {
if (idx.column() == 0 and role = Qt::DisplayRole)
return l_actions; // All the actions for drawing.
else
return QVariant();
} else // identity proxy case
return base_t::data(idx, role);
}
Qt::ItemFlags flags(QModelIndex const& idx) const
{
if (!l_actions.empty() and idx.column() == 0)
return Qt::NoItemFlags; // No editable or selectable
else
return base_t::flags(idx);
}
// And here, I think, is where the fun starts:
// The action could be added before or after the sourceModel
// is set or this model is connected to a view, but I don't
// how that cases are supposed to be managed.
void add_action(QVariant const& action)
{
bool was_empty = l_actions.empty();
l_actions << action;
if (was_empty and !this->insertColumns(0, 1))
throw std::logic_error("Something went wrong");
Q_EMIT this->dataChanged
(this->createIndex(0, 0),
this->createIndex(this->rowCount(), 0),
{ Qt::DisplayRole });
}
};
Without setting actions, the model works fine, both with QAbstractIdentityProxyModel and QSortFilterProxyModel as proxy_model. But, when setting actions, the view shows every cell blank, both with QSortFilterProxyModel and QAbstractIdentityProxyModel.
Here is a user-land code:
enum sql_action { DELETE };
auto* table_model = /* My QSqlTableModel */;
auto* view_model = new action_model<QIdentityProxyModel>(my_parent);
auto* table_view = new QTableView;
view_model->add_action(static_cast<int>(sql_action::DELETE));
view_model->setSourceModel(table_model);
table_view->setModel(view_model);
table_view->setSortingEnabled(true);
table_view->setAlternatingRowColors(true);
// The last column is printed in white, not with alternate colors.
table_view->show();
table_model->select();
The delegates are not a problem because I have set no one. I expect a first column with white cells, but I get an entirely white table. The column names are shown fine, except the last one, which prints just 0 as column name.
What am I doing wrong?
The problem is in your data() method.
You do not compare role to Qt::DisplayRole you assign to role
If you have actions, you either return the action entry or QVariant(), never any data

Qt: How to delete locally created multiple QIntValidator pointer object implemented on QlineEdit?

As per the Title, How to delete mulitple QIntValidator pointer Object created locally.I am stuck with an issue of memory leak.
I have a function as below:
void ABC::setTableDataItems(QStringList headerData)
{
int row = headerData.size();
int column = 0;
if (_txtLineEdit != NULL) {
delete _txtLineEdit;
_txtLineEdit = 0;
}
for (int i = 0; i < row ; i++)
{
_txtLineEdit = new QLineEdit();
_txtLineEdit->setMaxLength(_pktStruct[i].ItemDataLength);
_txtLineEdit->setText(headerData.at(i));
_pktStruct[i].currentLine = _txtLineEdit;
QString regExp = "[01]{1,";
regExp.append(QString("%1").arg(_pktStruct[i].ItemDataLength)).append("}");
long maxDigit = getMaxValueForDigit( _pktStruct[i].ItemDataLength );
QIntValidator* decValidator = new QIntValidator( 0, maxDigit, _txtLineEdit );
QRegExpValidator* binValidator = new QRegExpValidator(QRegExp(regExp),_txtLineEdit);
switch (_pktStruct[i].ItemDataType)
{
case DATA_TYPE_ASCII:
break;
case DATA_TYPE_HEX:
break;
case DATA_TYPE_NUM:
_txtLineEdit->setValidator(decValidator);
break;
case DATA_TYPE_BINARY:
_txtLineEdit->setValidator(binValidator);
break;
case DATA_TYPE_MAX:
break;
}
ui->pcusim_cmd_task_tableWidget->setCellWidget(i, column, _txtLineEdit);
connect(_txtLineEdit, SIGNAL(textChanged(QString)), this, SLOT(on_pcusim_cmd_task_tableWidget_linedit_cellChanged(QString)));
}
}
In above function, I need to delete all multiple QIntValidator created dynamically (inside For Loop ) every time before the loop when the above function is called.
Don't know the way. Please give some suggestions / idea to proceed further ??
Thanks in advance
Why don't you do
QValidator* pValidator = NULL;
switch (_pktStruct[i].ItemDataType)
{
case DATA_TYPE_NUM:
// create the proper int validator here
// ...
pValidator = new QIntValidator(0, maxDigit, _txtLineEdit);
break;
case DATA_TYPE_BINARY:
// create the proper regexp validator here
// ...
pValidator = new QRegExpValidator(QRegExp(regExp),_txtLineEdit);
break;
}
_txtLineEdit->setValidator(pValidator);
That way you don't create validators that are not used.
Since you pass _txtLineEdit as parent on construction of validators, they will be deleted when their parent QLineEdit object is destroyed.
By the way, setCellWidget() takes ownership of the widget, so you don't need to delete _txtLineEdit; (assuming this is the one you passed to it, you should have created a list of them in this case)

Resources