缩放QTableWidget

时间:2014-11-12 10:48:40

标签: c++ qt scale qtablewidget

我需要缩放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);
}

仅重新调整边框:

A scaled QTableWidget

我在QTableWidgetItem上看不到任何paintEvent,那么如何重新调整我的所有表?

提前谢谢你,祝你有个美好的一天。

编辑----------------------------------------

这种行为可能看起来很奇怪,所以这里有一些解释:

此QT窗口是acrobat阅读器窗口的子项。当我在acrobat上取消缩放PDF时,我调整了QT窗口的大小以保持相同的比例,但我想缩放窗口的内容。

示例:如果我取消缩放我的PDF,我会减小QT窗口的大小以保持相同的比例,并且我想将我的QT窗口的内容缩放到这个新大小(减小显示大小,就像unzoom一样) 。清楚吗 ? :○

但是例如视图不适合窗口,我有这个:

result

我想要这个:

wanted result

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

wanted result after a zoom

非常感谢你的帮助和时间。

1 个答案:

答案 0 :(得分:1)

QGraphicsSceneQTableWidget一起使用,但这并不困难:

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_H

.cpp的:

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

您想要的结果:

enter image description here

请注意,用户仍然可以编辑数据等,功能没有改变。

附加示例,例如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();
}

结果:

enter image description here

如您所见,用户可以缩放表格,编辑数据和信号以及插槽工作。一切都很好。