How to insert QPushButton into TableView? - qt

I am implementing QAbstractTableModel and I would like to insert a QPushButton in the last column of each row. When users click on this button, a new window is shown with more information about this row.
Do you have any idea how to insert the button? I know about delegating system but all examples are only about "how to edit color with the combo box"...

You can use
QPushButton* viewButton = new QPushButton("View");
tableView->setIndexWidget(model->index(counter,2), viewButton);

The model-view architecture isn't made to insert widgets into different cells, but you can draw the push button within the cell.
The differences are:
It will only be a drawing of a pushbutton
Without extra work (perhaps quite a bit of extra work) the button won't be highlighted on mouseover
In consequence of #1 above, you can't use signals and slots
That said, here's how to do it:
Subclass QAbstractItemDelegate (or QStyledItemDelegate) and implement the paint() method. To draw the pushbutton control (or any other control for that matter) you'll need to use a style or the QStylePainter::drawControl() method:
class PushButtonDelegate : public QAbstractItemDelegate
{
// TODO: handle public, private, etc.
QAbstractItemView *view;
public PushButtonDelegate(QAbstractItemView* view)
{
this->view = view;
}
void PushButtonDelegate::paint(
QPainter* painter,
const QStyleOptionViewItem & option,
const QModelIndex & index
) const
{
// assuming this delegate is only registered for the correct column/row
QStylePainter stylePainter(view);
// OR: stylePainter(painter->device)
stylePainter->drawControl(QStyle::CE_PushButton, option);
// OR: view->style()->drawControl(QStyle::CE_PushButton, option, painter, view);
// OR: QApplication::style()->drawControl(/* params as above */);
}
}
Since the delegate keeps you within the model-view realm, use the views signals about selection and edits to popup your information window.

You can use setCellWidget(row,column,QWidget*) to set a widget in a specific cell.

Related

How To Create Tri-state Actions on Menus Using Qt

I have a situation where I need a tri-state checkable action on a QMenu, and the QAction class appears to only support on or off, unless I'm overlooking something obvious. The situation I have is a context menu where multiple objects are selected, but may have different states for a given boolean condition (i.e. for a given property, some objects have a true value and some false). I can leave the action unchecked when there's a mix of values, but I feel that's misleading to the user.
I'm using Qt 5.5.1 and very experienced with Qt, but not seeing a way to achieve what I want in this instance.
There is no direct way to do it from a QAction, but it is possible to subclass the QWidgetAction and create an action that contains a QCheckBox widget.
class CheckBoxAction : public QWidgetAction {
public:
CheckBoxAction (const QString &text) : QWidgetAction(Q_NULLPTR) {
QHBoxLayout *_layout = new QHBoxLayout(Q_NULLPTR);
QWidget *_widget = new QWidget(Q_NULLPTR);
QLabel *_label = new QLabel(text);
mCheckbox = new QCheckBox(Q_NULLPTR);
_label->setAlignment(Qt::AlignLeft);
_layout->addWidget(mCheckbox);
_layout->addWidget(_label);
_layout->addStretch();
_widget->setLayout(_layout);
setDefaultWidget(_widget);
}
QCheckBox *checkbox() {
return mCheckbox;
}
private:
QCheckBox *mCheckbox;
};
Simply use the class to then add to your menu; for instance:
CheckBoxAction *checkAction = new CheckBoxAction(QStringLiteral("My Action"));
checkAction->checkbox()->setCheckState(Qt::PartiallyChecked);
menu->addAction(checkAction);
You can use the checkbox() method in order to connect to signals when it changes; or change the checkbox state. Hope this helps.

Is it possible to use busy progressbar with normal functionality in QListView?

I have created by myself model (MyListModel) that store list of my objects MyObj. MyObj stores some text information. Then I created class ItemDelegate, in order to manage view of every item and also to add QProgressBar to each item. The problem is that I need busy progressbar, but when I execute app, QProgressBar doesn't do any action.
I'm guessing, it's because QListView only show static data. Is there any way to make it work properly?
My realisation of paint method for Delegate:
void StatusItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
StatusItem my_item = index.data().value<StatusItem>();
wdg->name->setText(my_item.m_name);
switch (my_item.m_state)
{
case 0: wdg->progress->setMaximum(0); wdg->progress->setMinimum(0); break;
case 1: wdg->progress->setRange(0, 0); wdg->progress->setValue(100); break;
case 2: wdg->progress->setHidden(true); break;
}
QPalette pal;
QLinearGradient gradient(0, 0, 0, 100);
if ((option.state & QStyle::State_Selected) == QStyle::State_Selected)
{
pal.setBrush(QPalette::Window, QBrush(QColor(0, 255, 200)));
}
else
{
gradient.setColorAt(0.0, QColor(255,250,0));
gradient.setColorAt(1.0, QColor(255,255,255));
pal.setBrush(QPalette::Window, QBrush(QColor(Qt::transparent)));
pal.setBrush(QPalette::Window, QBrush(gradient));
}
wdg->setPalette(pal);
wdg->resize(option.rect.size());
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->translate(option.rect.topLeft());
wdg->render(painter);
painter->restore();
}
I think the problem exists because the view repaints items only when something is changed. While model data remains unchanged, according cells are not repainted.
In this situation you should cause additional repaints from the delegate. Unfortunately there is no convenient way to do it, but there are longer ways. You need to do the following action in the paint method:
view->update(index);
There are two issues:
You don't have view pointer in the paint method. You can either try to use painter->device() and assume that this is the view's viewport (but it's not guaranteed) or store view in the delegate's class member variable.
Calling update inside paint can cause infinite recursion (I'm not sure about this one). If that's the case, you can delay update using QTimer::singleShot(0, ...). But you will need to create a slot for that somewhere an pass view and index somehow so that they will be accessible in the slot.

Esc key press event for QListView

I created a simple QListView added a QStringListModel that allow items to be added and their text edited. But I don't want to allow empty fields to be added and I partially achieved this by using the dataChanged signal from the model which is emitted if the list item loses focus without text inserted or the user presses the Enter key without adding text.
However if you press the Esc key, the field remains empty and no dataChanged signal is emitted. How can I get notified if the field was left empty without overloading the QListView class which would be tedious(I used the designer to create the form)?
Is there another signal that is emitted or method I can use to achieve that?
Thanks!
Use event filters in your main GUI class:
void GUI::GUI()
{
ui->mListView->installEventFilter(this);
}
bool GUI::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->mListView && event->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
if (ke->key() == Qt::Key_Escape)
// special Esc handling here
}
else
return false;
}
It's simple to use your custom widgets in Qt Designer. Right click on the QListView and choose Promote to ... there add a new class and apply it to the widget.
http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html

How to customize QCombobox with multiple comlumns

I am using QCombobox, i want to every item in QCombobox displays three icons. But currently, every item in QCombobox only displays one icon!
Every icon should be changed dynamically.
You should create new custom QAbstractItemDelegate and set it to QComboBox using void QComboBox::setItemDelegate ( QAbstractItemDelegate * delegate ) api.
In delegate, you need to implement
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0
as you required.
You will also need to use following API to provide different icon to combo box in userData, that you can use in deletegate's paint method to retrieve icon and draw it.
void QComboBox::addItem ( const QString & text, const QVariant & userData = QVariant() )
Summary:
When I implement as above, there icons only show as drop down list clicked. In normal situation, the text only show. So, for three icons and text show in normal situation we must reimplement paintEvent of QCombobox in case subclass QCombobox or using eventFilter to catch paintEvent of QCombobox without subclass QComboBox!
Thank for your all response!
Reimplement paintEvent, or use big icon image with all 3 icons on it.

Simple way to get all visible items in the QListView

I am trying to develop an image gallery application using Qt Framework. The application loads all the images from the selected folder and those images are displayed using QListView control.
But now i want to reduce the memory consumption by loading only the images that are visible to user. Since there is no direct function to get all the visible items in the view, i am not able to achieve this.
You can get the visible items of a list view using the indexAt function. For more details and an example you can check the following article:
http://qt-project.org/faq/answer/how_can_i_get_hold_of_all_of_the_visible_items_in_my_qlistview
I found it! You have to connect the vertical scrollbar of the listwidget to a signal:
connect(ui->listWidget->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(launch_timer()));
Every time the user scrolls, the valuechanged(int) signal is being omitted! The thing is that you shouldn't run the code provided by webclectic in this question every time the value of the vertical scrollbar of the listwidget changes, because the program will be unresponsive with so much code to run in so little time.
So, you have to have a singleshot timer and point it to the function that webclectic posted above. When launch_timer() is called, you do something like this:
if(timer->isActive()){
timer->stop();
timer->start(300);
}
else
timer->start(300);
and the timeout() signal of timer will be connected to the slot webclectic talked about. This way, if the user scrolls quickly all the way down only the last items will be updated. Generally, it will be updated anything visible for more than 300 milliseconds!
I think what you need is to implement your own model (take a look to the QAbstractListModel documentation) so that way you could decide when you have to load more images to show and maybe free some of the images that became non-visible.
although this is not so simple in Qt 4 but
it is always simple to copy below:
#include <private/qlistview_p.h>
class QListViewHelper : public QListView
{
typedef QListView super;
inline QListViewHelper() {} //not intended to be constructed
public:
inline static QVector<QModelIndex> indexFromRect(const QListView *view,
const QRect &rect)
{
const QListViewPrivate *d = static_cast<const QListViewPrivate *>(QObjectPrivate::get(view)); //to access "QListViewPrivate::intersectingSet(...)"
const QListViewHelper *helper = static_cast<const QListViewHelper *>(view); //to access "QListView::horizontalOffset()"
return d->intersectingSet(rect.translated(helper->horizontalOffset(), helper->verticalOffset()), false);
}
inline static QVector<QModelIndex> visibleItems(const QListView *view)
{ return indexFromRect(view, view->rect()); }
inline static QModelIndex firstVisible(const QListView *view)
{ return visibleItems(view).value(0); }
inline static QModelIndex lastVisible(const QListView *view) {
const QVector<QModelIndex> &items = visibleItems(view);
return items.value(items.count() - 1);
}
};
void ourTest(const QListView *view) {
QModelIndex &index = QListViewHelper::firstVisible(view);
qDebug("QListViewHelper: first visible row is %d", index.row());
index = QListViewHelper::lastVisible(view);
qDebug("QListViewHelper: last visible row is %d", index.row());
}
usage:
QModelIndex &index =
QListViewHelper::firstVisible(listViewPointerHere)
note: since it does use Qt 4.8 private-headers it may no longer work in latter versions and will need some changes.
You can keep track of all the elements that are drawn per paint event. I used a delegate and overloaded the paint event.
I also overloaded the paint event in the view. During this call, all the visible delegates will get a paint event.
If you just need to know if an item is visible, you can increment a frame count in view->paintEvent and set that number in the delegate item. The item is visible of the item matches the current frame number.
If you need a list of all visible items, clear the visible item list in view->paintEvent and add each item in the int the delegate->paintEvent to the visible items list.

Resources