我需要缩放一个QtableWidget (行为应该像缩放一样)。
但是当我重新实现QtableWidget的paintEvent并手动设置比例时,如下所示:
void MyTableWidget::paintEvent ( QPaintEvent * event )
{
QTableWidget::paintEvent(event);
QPainter p(viewport());
p.scale(m_scale,m_scale);
p.drawRect( 0, 0, width()-1, height()-1);
}仅重新缩放边框:

而且我在QTableWidgetItem上看不到任何paintEvent,那么我如何重新缩放我的所有表?
提前谢谢您,祝您有愉快的一天。
编辑
这种行为可能看起来很奇怪,所以这里有一些解释:
此QT窗口是acrobat阅读器窗口的子窗口。当我在acrobat上取消缩放PDF时,我调整了QT窗口的大小以保持相同的比例,但我希望缩放窗口的内容。
例如:如果我取消缩放PDF,我会减小QT窗口的大小以保持相同的比例,并且我希望将QT窗口的内容缩放到这个新的大小(减小显示大小,就像取消缩放一样)。清楚了吗?:o
但是例如视图不适合窗口,我有这个:

我想要的是:

当我放大我的PDF时,我会增加窗口大小并放大内容,如下所示:

非常感谢您的帮助和您的时间。
发布于 2014-11-12 20:16:33
在QTableWidget中使用QGraphicsScene,但这并不是很困难:
QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QTableWidget *wgt = new QTableWidget;
wgt->setColumnCount(10);
wgt->setRowCount(10);
for (int ridx = 0 ; ridx < wgt->rowCount() ; ridx++ )
{
for (int cidx = 0 ; cidx < wgt->columnCount() ; cidx++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(QString("%1").arg(ridx));
wgt->setItem(ridx,cidx,item);
}
}
QGraphicsProxyWidget *pr = scene->addWidget( wgt );
pr->moveBy(10,10);缩放视图:
ui->graphicsView->scale(2,2);更好的放大方式是用滚轮缩小。子类视图或使用eventFilter。例如:
标题:
#ifndef MYQGRAPHICSVIEW_H
#define MYQGRAPHICSVIEW_H
#include <QGraphicsView>
class MyQGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
explicit MyQGraphicsView(QWidget *parent = 0);
signals:
protected:
void wheelEvent(QWheelEvent* event);
};
#endif // MYQGRAPHICSVIEW_HCpp:
#include "myqgraphicsview.h"
#include <QPointF>
MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
}
void MyQGraphicsView::wheelEvent(QWheelEvent* event) {
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
// Scale the view / do the zoom
double scaleFactor = 1.15;
if(event->delta() > 0) {
// Zoom in
scale(scaleFactor, scaleFactor);
} else {
// Zooming out
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
// Don't call superclass handler here
// as wheel is normally used for moving scrollbars
}用法:
MyQGraphicsView *view = new MyQGraphicsView;
view->setScene(scene);//same scene
view->show();您想要的结果:

请注意,用户仍然可以编辑数据等,功能没有改变。
额外的例子,就像在Qt书中一样。
(相同的MyQGraphicsView类)
#include "myqgraphicsview.h"
#include <QGraphicsProxyWidget>//and other needed includes
//just to show that signals and slots works
//with widget which placed in graphicsview
void print(int row, int column)
{
qDebug() << row+1 << column+1;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget = new QWidget;//container
QVBoxLayout *lay = new QVBoxLayout;
MyQGraphicsView * view = new MyQGraphicsView;//view as part of UI
QGraphicsScene * scene = new QGraphicsScene;
QTableWidget *wgt = new QTableWidget;//table which will be added to graphicsView
QObject::connect(wgt,&QTableWidget::cellPressed,print);//connection using Qt5 style(and power)
wgt->setColumnCount(10);
wgt->setRowCount(10);
for (int ridx = 0 ; ridx < wgt->rowCount() ; ridx++ )
{
for (int cidx = 0 ; cidx < wgt->columnCount() ; cidx++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(QString("%1").arg(ridx));
wgt->setItem(ridx,cidx,item);
}
}
QPushButton *butt = new QPushButton("click");
lay->addWidget(view);
lay->addWidget(butt);
widget->setLayout(lay);
QGraphicsProxyWidget *pr = scene->addWidget( wgt );
pr->moveBy(10,10);
view->setScene(scene);
widget->show();
return a.exec();
}结果:

如你所见,用户可以缩放表格,编辑数据和信号,以及插槽工作。一切都好。
https://stackoverflow.com/questions/26885091
复制相似问题