如何知道QListWidgetItem是否被滚动隐藏? [英] How to know if QListWidgetItem is hidden by scroll?

查看:503
本文介绍了如何知道QListWidgetItem是否被滚动隐藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是Qt的新手,我遇到了问题.

Well, i'm newbie in Qt and i have a problem.

我的UI中有一个QListWidget,其中包含7个项目,仅显示4个项目,而另一个在使用滚动条后显示.我想在QListWidet上方和下方显示一个箭头图像,而不是要滚动的项目.

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.

好吧,我可以查看某个项目是否被隐藏,但是就好像它被setHidden()函数隐藏了一样.何时滚动隐藏它?我可以在运行时看到吗?因为项目在那里,但是滚动条将其隐藏了,对吗?

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...

非常感谢!

推荐答案

因此,这是有关如何执行此操作的基本示例.首先是MainWindow类的清单 实施:

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);

}

从代码中可以看到,我已将滚动条策略设置为Qt :: ScrollBarAsNeeded.通过这样做,我们可以利用以下事实:我们现在可以对滚动条中的显示/隐藏事件做出反应.这是QScrollBar的重新实现:

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);
}

现在,您可以将ScrollBar的showTip(bool)信号连接到绘制图像的插槽.

Now you can connect the showTip(bool) signal from the ScrollBar to the slot that draws the image.

这篇关于如何知道QListWidgetItem是否被滚动隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆