2016-08-03 326 views
0

這是從Qt QGraphicsScene add and delete lines的延續。我以爲我能做到,但我做不到。窗口顯示出來,但無論我檢查/取消選中多少次,座標軸只顯示一次,永遠不會隱藏或切換;他們在第一次「開始」檢查後繼續工作。Qt QGraphicsScene顯示/隱藏軸

我加了qDebug(toggled ? "1" : "0"),它正確地顯示了toggled的價值改變從01,反之亦然上的複選框每一次點擊,但他們開啓後軸上唯一留任。我一個星期前纔開始Qt(大約一個月前C++),此時我不知道該相信什麼。

在鏈接問題中,thuga在循環內部使用connect(),並且只處理一行,但該行被切換。我也試過使功能bool,返回toggled,但它是同樣的事情。這就好像setVisible(false)不起作用。

這裏是我(你想要的任何替換XY變量):

[公共槽]

void plotAxes() 
{ 
    QPen *dashedLine {new QPen(QBrush(Qt::blue), 1, Qt::DashLine)}; 
    QGraphicsLineItem *xAxis {scene->addLine(xMin, 0, xMax, 0, *dashedLine)}; 
    QGraphicsLineItem *yAxis {scene->addLine(yCenter, yMin, yCenter, yMax, *dashedLine)}; 
    bool toggled {axesToggle->isChecked()}; 
    if (!toggled) 
    { 
     xAxis->setVisible(false); 
     yAxis->setVisible(false); 
    } 
    else 
    { 
     xAxis->setVisible(true); 
     yAxis->setVisible(true); 
    } 
    scene->update(); 
} 

axesToggleQCheckBox,其connect()功能是:

connect(axesToggle, &QCheckBox::toggled, this, &PlotBox::plotAxes); 

其中PlotBox是班級。另外還有boxTogglegridToggle,但如果這不起作用,沒有意義。


[編輯]

爲了更好地展示我有,這裏的應用程序的截圖: qtapp

它開始用一個簡單的sinc()試圖QGraphicsView,然後轉移到一個八度的stem-like情節,然後我不斷加入練習Qt(我只是在學習它) - 現在忽略奇怪的虛線網格線。它目前的作用是在sinc() settings旋鈕選擇+/-點擊後自動更新圖形,或者選擇Plot style,它具有切換抗鋸齒的快捷鍵,可以用Ctrl+MouseWheel進行縮放,可以正確重置視圖。所以現在我想我會添加切換網格& co的可能性,同時也允許設置網格線。

現在,我設法通過放棄公共插槽功能並使connect()成爲thuga在前面的答案中做到的結果。這是在PlotBox()構造函數內(PlotBox是類)。

QPen *dashedLine {new QPen(QBrush(Qt::blue), 1, Qt::DashLine)}; 
QGraphicsLineItem *xAxis {scene->addLine(xMin, 0, xMax, 0, *dashedLine)}; 
QGraphicsLineItem *yAxis {scene->addLine(yCenter, yMin, yCenter, yMax, *dashedLine)}; 
connect(axesToggle, &QCheckBox::toggled, [=](bool toggled){xAxis->setVisible(toggled); yAxis->setVisible(toggled)}); 

和它的作品只有在xy變量是簡單的值(如在int a {value}),但它們都依賴於sinc() settings spinboxes;例如,我使用orderN = orderSet->value()來設置N,用於計算xy變量。因此,如果我不更改劇情,上述內容纔有效。

如果我希望他們能在同步與sinc() settings,然後我不得不作出這樣的:

orderN = static_cast<short>(orderSet->value()); 
spacingS = static_cast<short>(spacingSet->value()); 
plotHeight = static_cast<short>(heightSet->value()); 
int xMin {-spacingS}; 
int xMax {orderN*spacingS}; 
int yMin {plotHeight>>2}; 
int yMax {-plotHeight}; 
double yCenter {(orderN-1)*spacingS*0.5}; 
QPen *dashedLine {new QPen(QBrush(Qt::blue), 1, Qt::DashLine)}; 
QGraphicsLineItem *xAxis {scene->addLine(xMin, 0, xMax, 0, *dashedLine)}; 
QGraphicsLineItem *yAxis {scene->addLine(yCenter, yMin, yCenter, yMax, *dashedLine)}; 
connect(axesToggle, &QCheckBox::toggled, [=](bool toggled){xAxis->setVisible(toggled); yAxis->setVisible(toggled)}); 

,這也將工作,但只有當我不作任何改變任何sinc() settings紡絲箱。如果我嘗試切換軸,它會崩潰,因爲xMin & co被Plot style函數動態使用,當它們被調用時 - 它們都在本地修改這些值。這會導致值不匹配。

所以,如果只需要適應spinboxes值,然後我把它改造成它的功能,獨自一人,像這樣的:

void plotAxes(const bool &toggled) 
{ 
    orderN = static_cast<short>(orderSet->value()); 
    spacingS = static_cast<short>(spacingSet->value()); 
    plotHeight = static_cast<short>(heightSet->value()); 
    int xMin {-spacingS}; 
    int xMax {orderN*spacingS}; 
    int yMin {plotHeight>>2}; 
    int yMax {-plotHeight}; 
    double yCenter {(orderN-1)*spacingS*0.5}; 
    QPen *dashedLine {new QPen(QBrush(Qt::blue), 1, Qt::DashLine)}; 
    QGraphicsLineItem *xAxis {scene->addLine(xMin, 0, xMax, 0, *dashedLine)}; 
    QGraphicsLineItem *yAxis {scene->addLine(yCenter, yMin, yCenter, yMax, *dashedLine)}; 
     xAxis->setVisible(toggled); 
     yAxis->setVisible(toggled); 
     //qDebug(toggled ? "1" : "0"); // this correctly shows "toggled" changing values 
    scene->update(); 
} 

我與connect()這樣叫:

connect(axesToggle, &QCheckBox::toggled, [=](bool toggled){plotAxes(toggled);}); 

這一個使用與上述相同的xAxis->setVisible(toggle),但它不起作用。只有當Axes複選框被選中時,它纔會顯示軸,然後它們會一直保持,直到我修改了繪圖,然後再次打開,但就是這樣。

整個main.cpp長約370行。如果需要,我可以將它發佈到pastebin。這很可能是你見過的最幼稚的代碼,但我只是在學習。

回答

0

我管理通過使xAxisyAxis私有變量,然後使用成員函數updateValues()處理所有必要的其它變量,以及解決這個問題,這是由每個專用槽功能plotImpulses()plotLines()調用的函數,並且plotSteps(),從而重新使用的值,然後保持connect()爲:

connect(axesToggle, \ 
      &QCheckBox::toggled, \ 
      [=](bool toggled) 
      { 
       xAxis->setVisible(toggled); 
       yAxis->setVisible(toggled); 
      }); 

在構造函數中結束,之後我只需要調用默認的打印樣式(plotImpulses())爲了要更新的值。這應該只發生一次與構造函數(右?)。現在,只要圖中有變化,updateValues()通過繪圖樣式函數被調用,這意味着使用來自旋轉框的新的更新值來完成軸(+框+網格)的切換。我不確定它是如何正統的,或者有多少空間或優化空間,所以建議是非常受歡迎的。