简体   繁体   English

只需按一下即可将QToolButton移到不同的布局?

[英]Move QToolButton to different layouts by just pressing it?

I have here two different layouts, and one QToolButton. 我在这里有两种不同的布局,以及一种QToolButton。 My goal is to transfer that button between the two layouts when I click it. 我的目标是在单击该按钮时在两个布局之间转移该按钮。 I figured this code would work, 我认为这段代码可以工作,

snippet: 片段:

void DominionLinux::on_toolButton_clicked(string state)
{
    if (state=="Disabled"){
        ui->verticalLayout_Enabled->addWidget(ui->toolButton);
        state = "Enabled";
    }
    else if (state=="Enabled"){
        ui->verticalLayout_Disabled->addWidget(ui->toolButton);
        state = "Disabled";
    }
}

By default, state == "Disabled". 默认情况下,状态==“禁用”。 When I test the UI in QTCreator, the first time I click, it works; 当我第一次在QTCreator中测试UI时,它可以工作; the button dissapears from one template, and appears on the other. 该按钮将从一个模板中消失,并出现在另一个模板上。 The second time I click when its on the other template, it doesn't. 当我第二次单击另一个模板时,它没有。 When compiling, I get this warning: *QMetaObject::connectSlotsByName: No matching signal for on_toolButton_clicked(string)* 编译时,出现以下警告:* QMetaObject :: connectSlotsByName:没有匹配的信号on_toolButton_clicked(string)*

Any ideas why the slot stops working? 有什么想法为什么插槽停止工作?

Any ideas why the slot stops working? 有什么想法为什么插槽停止工作?

You are missing the signal declaration at the place of the connect as the warning also hints. 警告提示还提示您在连接位置缺少信号声明。 Also, it seems that you are passing either the slot as the signal to the connect method. 另外,似乎您正在将任一插槽作为信号传递给connect方法。 A signal should not have the same name as a slot in a Qt application. 信号的名称不应与Qt应用程序中的插槽名称相同。

Other than that, you may wanna reconsider your design about disabling and enabling a button. 除此之外,您可能想重新考虑有关禁用和启用按钮的设计。 Putting them into separate layers is not the appropriate way of doing it. 将它们放在单独的层中不是合适的方法。

Moreover, you should probably avoid raw strings for representing states in general. 此外,您可能应该避免使用原始字符串来表示状态。 It is better to use enumerations, or boolean for "toggle states". 最好使用枚举,或者将布尔值用于“切换状态”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM