2014-11-12 4 views
1

QtableWidget을 확장해야합니다 (동작은 확대/축소와 같아야 함).QtableWidget 크기 조정

하지만 QtableWidget의 paintEvent 다시 구현과 같이, 배율을 수동으로 설정하는 경우 :

A scaled QTableWidget

그리고 나는 어떤 표시되지 않는 :

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 Reader 창의 하위 노드입니다. Acrobat에서 PDF의 크기를 줄인 경우 QT 창의 크기를 동일하게 유지하기 위해 크기를 조정하지만 창의 내용을 크기를 조정하고 싶습니다.

예 : 내 PDF의 크기를 줄인 경우 QT 창의 크기를 줄여 동일한 비율로 유지하고 내 QT 창의 내용을이 새 크기로 확대 (축소 크기와 같이 표시 크기를 줄이려고합니다.). 명백합니까 ? I가 확대되면

wanted result

을 그리고 :

그러나이 예를 들어 뷰가 창에 맞게 donesn't O를,이 있습니다

result

그리고이 원하는 내 PDF, 창 크기를 늘리고 다음과 같이 내용을 확대합니다.

wanted result after a zoom

도움과 시간을 매우 보내 주셔서 감사합니다. 대신 QTableWidget

+0

테두리 만 재조정된다는 것은 QTableWidget :: paintEvent에서 사용되는 painter-obj가 사용자가 조정 한 것이 아니기 때문에 ... 나는 당신의 목표가 전혀 단순하지 않다고 생각합니다. QTableWidget의 전체 paintEvent를 "재 작성"하거나 QGraphicsScene 내의 QTableWidget을 사용하십시오. 두 작업 모두 많은 작업이 필요합니다. – Robert

답변

1

사용 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_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

당신이 볼 수있는 바와 같이, 사용자는 테이블 편집 데이터 및 신호와 슬롯 작동을 확장 할 수 있습니다. 괜찮아.

+0

감사합니다. @ 체르노빌 감사합니다! 내 아키텍처는 다음과 같습니다. 레이아웃이있는 QWidget 및이 레이아웃은 내 QTableWidget을 포함합니다. 내가해야 할 새로운 아키텍처는 무엇입니까? 내 QWidget을 QTableWidget을 포함하는 레이아웃이 포함 된 QGraphicsView로 대체해야합니까? – Slot

+1

@Akiat QGraphicsView - 뷰 내부 장면 - 레이아웃이없는 QTableWidget (내 답변 에서처럼) 또는 QGraphicsView - 뷰 내부 장면 - 레이아웃이있는 QWidget 및이 레이아웃의 QTableWidget. – Chernobyl

+0

정말 미안하지만 더 구체적으로 할 수 있습니까? 어떻게 위젯을 겹쳐 놨는지 이해가 안되니? – Slot

관련 문제