2013-04-16 80 views
0

我有一個類的TimeGraph與qwtplot。 此qwtplot顯示在QGraphicsView上。 qwtplot的方法resize en resizeEvent但我不明白如何使用它們。在QGraphicsView QwtPlot調整大小

TimeGraph::TimeGraph(QWidget* parent, int in_display_time, QwtText title) 
{ 
    display_time = in_display_time; 

    graph.setParent(parent); 
    graph.setFixedSize(parent->width() - 20, parent->height() - 20); 
    graph.move(QPoint(10, 10)); 

    grid.attach(&graph); 
    curve.attach(&graph); 
    curve.setPen(QPen(Qt::red, 2)); 
    curve.setRenderHint(QwtPlotItem::RenderAntialiased); 

    if (title.text() != NULL) 
     graph.setTitle(title); 

    graph.setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(QDateTime(QDate(0, 0, 0), QTime(0, 0, 0, 0)))); 
    graph.setAxisScale(QwtPlot::xBottom, - display_time, 0); 
    graph.setAxisLabelRotation(QwtPlot::xBottom, -20.0); 
    graph.setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom); 

    graph.show(); 
} 

和插頭

struct Data { QVector<double> x; QVector<double> y;}; 

class TimeGraph 
{ 
private : 
    QwtPlot graph; 
    QwtPlotCurve curve; 
    QwtPlotGrid grid; 
    Data data; 
    int display_time; 

public: 
    TimeGraph(QWidget* parent, int in_display_time, QwtText title = QwtText()); 
    void addPoint(QDateTime date, double y); 
    void resize(QRect zone); 
}; 

而創建我圖作爲這樣的:

graph_O2 =新TimeGraph(UI-> graphicsView_graph_o2,120);

我會調整圖形大小調整自己的圖形大小。 我該怎麼辦?

回答

0

,這真是個必須把QWT​​情節上的QGraphicsView的頂部?我沒有使用Qwt一段時間,但AFAIK所有的繪圖都是在paintEvent中使用直線塗料完成的。 (這是爲什麼它如此之快的原因之一)。在QGraphicsView上使用Qwt控件的最合適的方式(直到我)都是使用忽略轉換標誌添加widget項目。 關於調整大小,如果你把Qwt放在平常的小部件裏面,它會自動處理所有的大小調整。如果你有一些複雜的自定義大小調整結構,你應該在父窗口小部件上處理resize事件,或者如果父類不可用,則通過eventFilter處理。

+0

不,qwt圖不一定真的在QGraphicsWiew上。但我不知道該怎麼做。我的qwt圖必須由代碼創建,因爲QtDesigner的插件qwt不適用於我的版本。沒有QGraphicsView如何將我的情節放置在我想要的QGroupBox中? – artoon

+0

我對示例做了另一個回答(參見上文) – evilruff

+0

好的,我的Widget中有我的TimeGraph。現在,我的類TimeGraph必須繼承QWidget嗎?並重新定義paintEvent? – artoon

0

我會建議最好的辦法是以下 - 讓你需要它除了QWT控制一切的UI。對於qwt控件,只需使用Widget或Frame(根據您的口味)預留空間。你可以設置你需要的所有類型的大小約束)。您可以從預覽中檢查它。設置一個小部件/框架名稱,讓你喜歡讓我們說m_fmQwtPlotContainer。 (你可以在QtDesigner中執行 - objectName)。然後你可以寫這樣的事情:

QWidget * pParent = findChild<QWidget*>("m_fmQwtPlotContainer"); 
QVBoxLayout * pLayout = new QVBoxLayout(pParent); 
pParent->setLayout(pLayout); 
..then your code using pParent as a parent for your widget.. 
TimeGraph * pGraph = new TimeGraph(pParent); 
pLayout->insertItem(pGraph);  

,所以你將有你的形式插在你喜歡

+0

好的,我的Widget中有我的TimeGraph。現在,我的類TimeGraph必須繼承QWidget嗎?並重新定義paintEvent? – artoon

0

一個地方,我都測試了我的兩個班TimeGraph

類TimeGraph的方法TimeGraph :公衆的QWidget {...}

TimeGraph :: TimeGraph(int in_display_time,QwtText title): QWidget(){ display_time = in_display_time;

graph.setParent(this); 
//graph.setMaximumSize(parent->width() - 20, parent->height() - 20); 
graph.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 

... }

與:

UI-> widget_graph_O2 =新TimeGraph(120);

該圖沒有顯示,因爲它具有不尺寸。

如果我測試第二個方法

類TimeGraph {...}

TimeGraph :: TimeGraph(QWidget的*父,INT in_display_time,QwtText title){ display_time = in_display_time;

graph.setParent(parent); 
//graph.setMaximumSize(parent->width() - 20, parent->height() - 20); 
graph.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); ... } 

graph_O2 =新TimeGraph(UI-> widget_graph_four,120);

該圖不隨框架調整大小。

我忘了什麼?

+0

使用方法2)更改graph.setParent(父) - > graph.setParent(this)並在構造函數之後添加QWidget(parent).. – evilruff

+0

但是我必須在我的TimeGraph上和我的小部件上設置尺寸和policySize ?因爲當我使用第二種方法與你的修改沒有任何顯示。但是,如果我不更改graph.setParent(父)我的圖形顯示 – artoon

+0

但您如何將窗口小部件添加到窗體? – evilruff

相關問題