I am using QListWidgetItem to add Items in my QListWidget.
In some situations, I want some rows of my QListWidget become non selectable. (I mean I want some QListWidgetItem to be non selectable)
Is ther any way to do this?
PS: I tried
listWidgetItem->setFlags(Qt::NoItemFlags)
listWidgetItem->setSelected(false);
but they don't disable the selection of items.
Edit:
QStringList _strListClients = _strClients.split(",",QString::KeepEmptyParts,Qt::CaseInsensitive);
for(int i = 0; i < _strListClients.count(); i++)//Add Client's Check Boxes
{
QListWidgetItem* _listWidgetItem = new QListWidgetItem(_strListClients[i], listWidgetClients);
listWidgetClients->addItem(_listWidgetItem);
if(_strListClients[i] == "Unknown"){
_listWidgetItem->setSelected(false);
_listWidgetItem->setTextColor(Qt::red);
_listWidgetItem->setFlags(_listWidgetItem->flags() & ~Qt::ItemIsSelectable);
}
}
Just remove the Qt::ItemIsSelectable flag from each item:
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
Or remove Qt::ItemIsEnabled if you want to remove all interaction with the item.
E.g.
#include <QtWidgets>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QListWidget widget;
for (int i = 0; i < 100; ++i) {
QListWidgetItem *item = new QListWidgetItem(QStringLiteral("Item %1").arg(i));
if (i % 2 == 0) // disable one every two
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
widget.addItem(item);
}
widget.show();
return app.exec();
}
You can try the following:
1) Override the clicked/selection event (sorry I don't remember the exact name.
Doing so you can have some sort of flag/bool value on the item, and if is set as not being selectable you just return.
2) Rather then override you just connect to the signal and perform the above check and if you don't want to select that item you un-select it afterwards.
A bit of a work around but I don't know if there is a built in way to do so.
Checking the documentation I did not see a disable method on the item itself.
If you go down the road of list view you should have more control on that, also on the display, so you might be able to displayed it greyed etc. A view is a bit more work though.
M.
Related
I have several QListWidgets and would like to only allow a single row to be selected for all of these lists. So for example if I select a row in one of the lists any other selection in the other lists would be cleared. How can I achieve this?
Is there a built-in way to do this (similar to QButtonGroup for buttons)? If not, what approach would you recommend that I take when trying to implement this myself?
Grateful for help and with kind regards,
Tord
AFAIK, there is no ready built-in feature to provide a single selection in multiple list views.
Instead, it can be done by a respective signal handler for the QSelectionModel::selectionChanged signal which does this whenever the selection of one of the involved list views changes.
Thereby, you have to consider that clearing the selection will emit a selectionChanged signal as well. (Otherwise, you may end up in a recursive call of your signal handler until a stack overflow occurs.)
Unfortunately, I use Qt in C++. (My Python knowledge is rather limited.)
Thus, all I can provide for now is my "proof of concept" in C++:
#include <QtWidgets>
void singleSel(QListView *pLstView, const QList<QListView*> &pLstViews)
{
for (QListView *pLstViewI : pLstViews) {
if (pLstViewI == pLstView) continue; // skip sender
// the check is necessary to prevent recursions...
if (pLstView->selectionModel()->hasSelection()) {
// ...as this causes emission of selectionChanged() signal as well:
pLstViewI->selectionModel()->clearSelection();
}
}
}
int main(int argc, char **argv)
{
qDebug() << "Qt Version: " << QT_VERSION_STR;
QApplication app(argc, argv);
// build contents
QStandardItemModel tblModel(0, 1);
for (int i = 0; i < 10; ++i) {
tblModel.appendRow(
new QStandardItem(QString::fromUtf8("Entry %0").arg(i + 1)));
}
// build some GUI
QWidget win;
QHBoxLayout qHBox;
QListView lstView1;
lstView1.setModel(&tblModel);
qHBox.addWidget(&lstView1);
QListView lstView2;
lstView2.setModel(&tblModel);
qHBox.addWidget(&lstView2);
QListView lstView3;
lstView3.setModel(&tblModel);
qHBox.addWidget(&lstView3);
win.setLayout(&qHBox);
win.show();
// install signal handlers
QList<QListView*> pLstViews = { &lstView1, &lstView2, &lstView3 };
QObject::connect(lstView1.selectionModel(),
&QItemSelectionModel::selectionChanged,
[&lstView1, &pLstViews](const QItemSelection&, const QItemSelection &)
{
singleSel(&lstView1, pLstViews);
});
QObject::connect(lstView2.selectionModel(),
&QItemSelectionModel::selectionChanged,
[&lstView2, &pLstViews](const QItemSelection&, const QItemSelection &)
{
singleSel(&lstView2, pLstViews);
});
QObject::connect(lstView3.selectionModel(),
&QItemSelectionModel::selectionChanged,
[&lstView3, &pLstViews](const QItemSelection&, const QItemSelection &)
{
singleSel(&lstView3, pLstViews);
});
// exec. application
return app.exec();
}
I compiled and tested on Windows 10 (64 bit). This is how it looks:
Update:
I tried to port the C++ application to Python/PyQt5:
#!/usr/bin/python3
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QListView
from PyQt5.QtGui import QStandardItemModel, QStandardItem
def singleSelect(lstView, lstViews):
for lstViewI in lstViews:
if lstViewI == lstView:
continue
# the check is necessary to prevent recursions...
if lstViewI.selectionModel().hasSelection():
# ...as this causes emission of selectionChanged() signal as well:
lstViewI.selectionModel().clearSelection()
if __name__ == '__main__':
app = QApplication(sys.argv)
# build contents
tblModel = QStandardItemModel(0, 1)
for i in range(0, 10):
tblModel.appendRow(QStandardItem("Entry %d" % (i + 1)))
# build GUI
win = QWidget()
hBox = QHBoxLayout()
lstView1 = QListView()
lstView1.setSelectionMode(QListView.SingleSelection)
lstView1.setModel(tblModel)
hBox.addWidget(lstView1)
lstView2 = QListView()
lstView2.setSelectionMode(QListView.SingleSelection)
lstView2.setModel(tblModel)
hBox.addWidget(lstView2)
lstView3 = QListView()
lstView3.setSelectionMode(QListView.SingleSelection)
lstView3.setModel(tblModel)
hBox.addWidget(lstView3)
win.setLayout(hBox)
win.show()
# install signal handlers
lstViews = [lstView1, lstView2, lstView3]
lstView1.selectionModel().selectionChanged.connect(lambda sel, unsel: singleSelect(lstView1, lstViews))
lstView2.selectionModel().selectionChanged.connect(lambda sel, unsel: singleSelect(lstView2, lstViews))
lstView3.selectionModel().selectionChanged.connect(lambda sel, unsel: singleSelect(lstView3, lstViews))
# exec. application
sys.exit(app.exec_())
From what I saw in the test, it behaves similar like the one written in C++.
The only exception is that I have to click twice on an entry when I change the list view.
As I already said: my experiences in PyQt are very limited. (Actually, I started today by making this sample.) Thus, I may have overseen something. Please, take it with a grain of salt...
Suppose I have a 2D array full of data say 10 x 10. The contents, as well as a number of rows, can change any time.
Now I want to display this data in a QTableWidget.
I use a timer with time out 1sec to refresh the table contents. In the timeout slot if I use
void slot_timeOut()
{
//Iterate over the rows
//and for each cell do something like
ui->tw_data->setItem(row, 0, new TableWidgetItem(data[row][0]);
ui->tw_data->setItem(row, 0, new TableWidgetItem(data[row][1]);
//...
ui->tw_data->setItem(row, 0, new TableWidgetItem(data[row][9]);
}
the use out new TableWidgetItem worries me. I have no reference to it and I never delete it.
Over a period of time is this a memory leak, or is this managed by Qt, pls help...
There is no leak, as
The table takes ownership of the item.
(From QTableWidget::setItem()).
Ownership here means that the QTableWidget will take care of deleting the item when its not longer needed, or the QTableWidget itself is destroyed. Ownership is usually documented in the Qt documentation (if not, I'd consider that a Qt bug).
Once setItem() returns from being called on the same cell, the previously set QTableWidgetItem will be deleted:
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QTableWidget widget(2, 1);
QTableWidgetItem* foo = new QTableWidgetItem("Foo");
widget.setItem(0, 0, foo);
qDebug() << foo->text(); //works
widget.setItem(0, 0, new QTableWidgetItem("Bar")); //replaces foo with bar and deletes foo
qDebug() << foo->text(); // Undefined (usually, crash)
widget.show();
return app.exec();
}
If you're on Linux, you can also verify the behavior by running above code (without the second qDebug()) in valgrind with --leak-check=full.
From the Qt documentation:
void QTableWidget::setItem ( int row, int column, QTableWidgetItem *
item ) Sets the item for the given row and column to item. The table
takes ownership of the item.
This indicates that Qt will manage the memory for the object as necessary.
It will delete properly when the you add Q-objects to the cells.
I was pushing thousands of C-strings in cells and watched my memory blow up.
Made my "raw text data" a QString and my problem was solved.
Such problem takes place to be: Qt does not really clear the memory allocated through QTableWidgetItem.
for( int row = 0; row < 35; row++)
{
for(int i = 0; i < 9; i++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(QString::number(i));
ui->tableWidget->setItem(row, i, item);
}
}
This code gives 116 bytes of leakage.
We found this solution: Instead of QTableWidgetItem, you can use QLineEdit:
QLineEdit* tableline = new QLineEdit();
m_qtablewidget->setCellWidget(row, column, tableline);
p.s. To search for leaks used Dr.memory
Recently I found the way that checkbox places in the middle of QtableWidget item.
However, I do not know how to check state whether or not button is clicked.
Could you tell me how to check button state?
here is what Ive found code:
QWidget *pWidget = new QWidget();
QCheckBox *pCheckBox = new QCheckBox();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
pCheckBox->setCheckState(Qt::Checked);
pLayout->addWidget(pCheckBox);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);
ui->tableWidget2->setCellWidget(2,2, pWidget);
Although this is very late you can solve it like this:
auto field = ui->tableWidget2->cellWidget(2, 2, pWidget);
std::cout << qobject_cast<QCheckBox*>(field)->isChecked() << std::endl;
This works for other types as well (QComboBox etc.). Although it would probably be better to just use the checkbox functionality that QTableWidgetItem already has.
This example might not work if you are using a tristate checkbox in which case you should call: checkState() and compare it to Qt::CheckState. If qobject_cast<T> does not work out you can use a reinterpret_cast<T>.
I assume you created your checkboxes in the QWidgetTable like this:
int row...;int column...;
...
QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
checkBoxItem->setCheckState(Qt::Unchecked);
ui->Table->setItem(row, column, checkBoxItem);
You can check the status of the item that corresponds to your widget in another function like this:
void MainWindow::on_Table_cellClicked(int row, int column)
{
QTableWidgetItem *checkBoxState = ui->Table->item(row, column);
if(ui->Table->item(row,column)->checkState())
{
checkBoxState->setCheckState(Qt::Unchecked);
ui->Table->setItem(row, column, checkBoxState);
}
else
{
checkBoxState->setCheckState(Qt::Checked);
ui->Table->setItem(row, column, checkBoxState);
}
}
In QT 4.7, I am trying to make one QTableWidgetItem in a QTableWidget Editable and the rest all columns should be read only for me. I am having problems here.
I have checked a number of samples through google and stackoverflow but failed to achieve this. Some of the options I tried are,
I create rows by calling insertRow(rownumber) for adding rows.
Trial 1: I do the following while inserting a row dynamically
Enable Edit triggers in the UI Dialog
Add columns using the following code for disabling edit
QTableWidgetItem qit("");
qit.setflags(qit.flags() & ~Qt::ItemIsEditable)
qtable.setitem(row,column, &qit);
And for others columns I don't set the flags
This above approach did not work. I am able edit all columns (even the one I negated the editable option)
Trial 2:
Do all the above with just qtable.setEditTriggers(Qt::NoEditTriggers) and then set the columns editable wherever required.
But this option renders all columns non-editable.
But I don't see anyone complaining like this in any forums. So I must be making some stupid mistake.
Have someone come across such an issue, if yes please help by answering.
Working example of QTableWidget
First item in added row is editable, second one is not editable.
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Prepare layout
QMainWindow *window = new QMainWindow;
QTableWidget *tablewidget = new QTableWidget;
window->setCentralWidget(tablewidget);
// Add data
tablewidget->insertRow(0);
tablewidget->insertColumn(0);
tablewidget->insertColumn(1);
QTableWidgetItem *item;
item = new QTableWidgetItem("editable");
tablewidget->setItem(0,0,item);
item = new QTableWidgetItem("non editable");
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // non editable
tablewidget->setItem(0,1,item);
window->show();
return a.exec();
}
How can I get the texts of all the widgets in a QListWidget as a QList<QString>?
I can get the list of widget items like this:
QList<QListWidgetItem *> items =
ui->listWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
But that's not exactly what I want, I'd like the list of the widget text() properties.
There is no built-in function for that, you'll need to do it manually.
QList<QString> texts;
foreach(QListWidgetItem *item, items)
texts.append(item->text());
Or something like that.
int c = ui->listWidget->count();
for (int i = 0; i < c ; ++i){
QString s = QString::number(i);
QModelIndex *model_index = new QModelIndex(ui->listWidget->model()->index(i,0) ); //0th column since we have one cloumn in listwidget
QString q= model_index->data(Qt::DisplayRole).toString();
qDebug()<<q;
}