2016-08-03 322 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()通过绘图样式函数被调用,这意味着使用来自旋转框的新的更新值来完成轴(+框+网格)的切换。我不确定它是如何正统的,或者有多少空间或优化空间,所以建议是非常受欢迎的。