我在qt 5.10.1上使用c ++工作,我有一个动态创建其他小部件的小部件。
for(QString pics : nameVector)
{
QIcon* buttonIcon = new QIcon;
QImage image(QString("%1").arg(pics)); //QImage's Constructor takes a file path
image.scaled(9000, scrollerHeight*7 , Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap pixMap(QPixmap::fromImage(image).scaled(5000, scrollerHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation)); //8 will do it once scrollbar is removed.;
buttonIcon->addPixmap(QPixmap::fromImage(image).scaled(5000, scrollerHeight * m_scaleFactor, Qt::KeepAspectRatio, Qt::SmoothTransformation));
QPushButton *pixButton = new QPushButton;
pixButton->setIconSize(imageSize);
pixButton->setIcon(*buttonIcon);
pixButton->setObjectName(pics + "button");
ui.horizontalLayout_2->addWidget(pixButton); //addButton
QObject::connect(&pixButton, &pixButton->clicked(true),
&ui.tempOutputLabel, &fileFinder(pics);
}
小部件中的滚动区域随着每次新增加而水平变大 - 这是通过qt“水平策略:扩展”在qt designer中完成的。
我现在有一个滚动条,但我想要做的是移动鼠标(最好是按住鼠标右键)并拉动图像 - 就像在手机上滚动一样。
我已经开始添加鼠标移动和鼠标按钮事件
void JobPreviewWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) //najdeme najblizsi obrazok
;
if (event->button() == Qt::RightButton)
{
QPointF m_rightEnd = event->pos();
m_offset += m_rightEnd - m_rightStart;
}
}
void JobPreviewWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton)
{
m_rightStart = event->pos();
}
}
void JobPreviewWidget::mouseMoveEvent(QMouseEvent *event)
{
m_mousePoint = event->pos();
repaint(); //this is the problem for me
}
我不知道如何使用偏移量m_offset重新绘制水平布局。任何指针都表示赞赏。