我的代码出现在下面。当我尝试编译它时,我得到:
error: 29:38 comparison between distinct pointer types QAbstractButton and QPushButton* lacks a cast -> 'if (stdmetBox.clickedButton() == stdButton)'
系统:Amazon Kindle Fire 7"运行 Cyanogenmod 11.0 (Android 4.4.2 Kitkat) 编译器:G++/GCC(C4droid 插件版本 4.9.1 的 GCC) IDE:带有 SDL、GCC 和 Ministro 插件的 C4droid
如有任何帮助,我们将不胜感激。我已经搜索过,但我能找到的唯一实例是特定于 iOS 的。
#include <fstream>
#include <QApplication>
#include <QLabel>
#include <QMessageBox>
#include <QString>
#include <QAbstractButton>
#include <QInputDialog>
#include <QDebug>
using namespace std;
int setup()
{
string unitchar;
string unitcharo;
bool setupSuccess;
int returncode;
QMessageBox msgBox;
QMessageBox stdmetBox;
QMessageBox ynBox;
msgBox.setText("Welcome to Tyler's Fitness App! This app will help you with your fitness goals, whatever they may be. Let's get you set up!");
msgBox.exec();
QPushButton *stdButton = stdmetBox.addButton(QT_TR_NOOP("Standard"), QMessageBox::ActionRole);
QPushButton *metButton = stdmetBox.addButton(QT_TR_NOOP("Metric"), QMessageBox::ActionRole);
stdmetBox.exec();
if (stdmetBox.clickedButton() == stdButton)
{
unitchar = "standard";
unitcharo = "metric";
}
else if (stdmetBox.clickedButton() == metButton)
{
unitchar = "metric";
unitcharo = "standard";
}
else
{
setupSuccess = false;
returncode = 0;
return returncode;
}
if (unitchar == "standard")
{
ynBox.setInformativeText("Standard units include feet, miles, mph, pounds, etc.");
}
else
{
ynBox.setInformativeText("Metric units include meters, kilometers, kph, kilograms, etc.");
}
QPushButton *yesButton = ynBox.addButton(QT_TR_NOOP("Yes"), QMessageBox::ActionRole);
QPushButton *noButton = ynBox.addButton(QT_TR_NOOP("No"), QMessageBox::ActionRole);
ynBox.setText("Is that okay?")
while (ynBox.clickedButton() == noButton)
{
if (unitchar == "standard")
{
unitchar = "metric";
unitcharo = "standard";
}
else
{
unitchar = "standard";
unitcharo = "metric";
}
if (unitchar == "standard")
{
ynBox.setInformativeText("Standard units include feet, miles, mph, pounds, etc.");
}
else
{
ynBox.setInformativeText("Metric units include meters, kilometers, kph, kilograms, etc.");
}
result = ynBox.exec();
}
if (unitchar == "standard")
{
msgBox.setText("Great! You've selected standard units.");
msgBox.exec();
setupSuccess = true;
}
else
{
msgBox.setText("Great! You've selected metric units.");
msgBox.exec();
setupSuccess = true;
}
if (setupSuccess != true)
{
returncode = 0;
}
else if (unitchar == "standard")
{
returncode = 1;
}
else
{
returncode = 2;
}
return returncode;
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
unitmaster = setup();
if ((unitmaster == 1) || (unitmaster == 2))
{
QMessageBox successBox;
successBox.setText("The program has completed successfully!");
successBox.exec();
}
else
{
QMessageBox errorBox;
errorBox.setText("The program has completed successfully!");
errorBox.exec();
}
return app.exec();
}
最佳答案
您似乎忘记包含 <QPushButton>
即使你使用 QPushButton
以一种需要它的定义的方式。其中一个 header 必须声明它,因为它被识别为一种类型,但如果它不完整,那么编译器就不会知道 QPushButton*
可转换为 QAbstractButton*
.
作为一般规则,请始终包含定义您使用的类型的 header ,除非您确定声明就足够了。基指针的隐式转换是需要定义的东西。
关于android - QAbstractButton 和 QPushButton* 之间的比较缺少强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29649546/