這是從Qt QGraphicsScene add and delete lines的延續。我以爲我能做到,但我做不到。窗口顯示出來,但無論我檢查/取消選中多少次,座標軸只顯示一次,永遠不會隱藏或切換;他們在第一次「開始」檢查後繼續工作。Qt QGraphicsScene顯示/隱藏軸
我加了qDebug(toggled ? "1" : "0")
,它正確地顯示了toggled
的價值改變從0
到1
,反之亦然上的複選框每一次點擊,但他們開啓後軸上唯一留任。我一個星期前纔開始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();
}
axesToggle
是QCheckBox
,其connect()
功能是:
connect(axesToggle, &QCheckBox::toggled, this, &PlotBox::plotAxes);
其中PlotBox
是班級。另外還有boxToggle
和gridToggle
,但如果這不起作用,沒有意義。
[編輯]
它開始用一個簡單的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。這很可能是你見過的最幼稚的代碼,但我只是在學習。