问题
Well, i'm newbie in Qt and i have a problem.
I have a QListWidget in my UI with 7 items, just 4 items are showed and the other are showed after to use scrollbar. I want to show a arrow image above and below QListWidet than will show than there are more items to scroll.
Ok, i can to see if a item is hidden, but just if it is hidden by setHidden() function. And when is it hidden by scroll? Can i see this in run time? Because the item is there, but scroll is hiding it, right?
I searched some post about this here, but i did not find. Sorry my english, maybe be confused, but if someone to can help me...
Thanks a lot!
回答1:
So Here is basic example on how to do it. First here is the listing of the MainWindow class implementation:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QListWidget *listW = new QListWidget;
//Add some items
for(int i = 0; i < 20; i++) {
QListWidgetItem *item = new QListWidgetItem("Item" + QString::number(i));
listW->addItem(item);
}
listW->setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
listW->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
//Set reimplemented scroll bar
listW->setVerticalScrollBar(new ScrollBar);
setCentralWidget(listW);
}
As you can see from the code I have set the scroll bar policy to Qt::ScrollBarAsNeeded. By doing that we can take advantage of the fact that we can now react on show/hide events from the scrollbar. And here is reimplementation of the QScrollBar:
ScrollBar::ScrollBar(QWidget *parent) :
QScrollBar(parent)
{
}
void ScrollBar::hideEvent(QHideEvent *e)
{
emit showTip(false);
}
void ScrollBar::showEvent(QShowEvent *e)
{
emit showTip(true);
}
Now you can connect the showTip(bool) signal from the ScrollBar to the slot that draws the image.
来源:https://stackoverflow.com/questions/8992034/how-to-know-if-qlistwidgetitem-is-hidden-by-scroll