我目前正在使用qt为大学课程编写旅行推销员问题的版本。为了将城市放置在QGraphicsView窗口小部件上,我创建了该程序,就像在YouTube教程中一样: https://www.youtube.com/watch?v=hgDd2QspuDg
可悲的是,我只是使所有的想法都像教程中那样,但是只有我得到错误,我的类是Abstract,我不理解,因为我在代码中定义的该类中的所有内容都不会在我的类中被引用
Dialog.h:
int main() {
int i,j,len;
int array[10],output[10];
printf("Enter 10 digit number separated by space: \n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
len = sizeof(array)/sizeof(array[0]);
for(i=0; i<len; i++)
{
if(array[i] != 0)
{
for(j=0; j<array[i]; j++)
//output[j] = i;
printf("%d ", i);
}
}
mycity.h:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QMessageBox>
#include <iostream>
#include <QPointF>
#include "mycity.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
private slots:
double on_Kelvin_box_valueChanged();
double on_Iter_box_valueChanged();
double on_Radius_box_valueChanged();
double on_CVRatio_box_valueChanged();
void on_resetButton_clicked();
void on_startButton_released();
void on_stopButton_released();
private:
Ui::Dialog *ui;
QGraphicsScene *scene;
MyCity *city;
protected:
};
#endif // DIALOG_H
Dialog.cpp:
#ifndef MYCITY_H
#define MYCITY_H
#include <QPainter>
#include <QGraphicsItem>
#include <QDebug>
#include <string.h>
class MyCity : public QGraphicsItem
{
public:
MyCity();
QRectF boundingRect() const;
void paint(QPainter *painter, QStyleOptionGraphicsItem *option, QWidget *widget);
bool Pressed;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};
#endif // MYCITY_H
main.cpp:
#include "dialog.h"
#include "ui_dialog.h"
#include <qtCore>
#include <qtGui>
#include <QPointF>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
city = new MyCity();
scene->addItem(city);
}
Dialog::~Dialog()
{
delete ui;
}
double Dialog::on_Kelvin_box_valueChanged()
{
double kelvin = ui->Kelvin_box->value();
return kelvin;
}
double Dialog::on_Iter_box_valueChanged()
{
double iter = ui->Iter_box->value();
return iter;
}
double Dialog::on_CVRatio_box_valueChanged()
{
double cvRatio = ui->CVRatio_box->value();
return cvRatio;
}
double Dialog::on_Radius_box_valueChanged()
{
double radius = ui->Radius_box->value();
return radius;
}
void Dialog::on_resetButton_clicked()
{
ui->Kelvin_box->setValue(0.3);
ui->Iter_box->setValue(10000);
ui->CVRatio_box->setValue(2.5);
ui->Radius_box->setValue(0.1);
}
//Wenn dieser Knopf gedrückt wird startet die Iteration
void Dialog::on_startButton_released()
{
}
//Wenn dieser Knopf gedrückt wird beendet sich die Iteration
void Dialog::on_stopButton_released()
{
}
mycity.cpp:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
这是我得到的错误:
C:... \ dialog.cpp:16:错误:抽象类类型'MyCity'的新表达式无效 city = new MyCity();
有人可以帮助我找到导致此抽象类的错误吗?
编辑: 这是我从qt收到的整个错误消息:
#include "mycity.h"
//Das is das Objekt 'Stadt' welches wir erzeugen in der dialog.cpp
MyCity::MyCity()
{
Pressed = false;
setFlag(ItemIsMovable);
}
QRectF MyCity::boundingRect() const
{
return QRectF(0,0,20,20);
}
void MyCity::paint(QPainter *painter, QStyleOptionGraphicsItem *option, QWidget *widget)
{
//ellipse wird rot bei draufdrücken, grün bei loslassen
QRectF rec = boundingRect();
QBrush brush(Qt::green);
if(Pressed)
{
brush.setColor((Qt::red));
}
else
{
brush.setColor((Qt::green));
}
painter->fillRect(rec,brush);
painter->drawRect(rec);
}
void MyCity::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//hier geschieht, was beim mausdruck passieren soll (stadt kreieren)
Pressed = true;
update();
QGraphicsItem::mousePressEvent(event);//überschreiben der eigentlichen mousePressEvent-funktion
}
void MyCity::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
//nur temporäre testfunktion, wird am ende nich gebraucht
Pressed = false;
update();
QGraphicsItem::mouseReleaseEvent(event);
}
答案 0 :(得分:4)
在覆盖虚拟方法时,始终使用override
关键字。如果您在编写MyCity::paint
时这样做,那么您将意识到它与QGraphicsItem::paint
的签名不匹配:
QGraphicsItem:
virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) = 0;
我的城市:
void paint(QPainter *painter, QStyleOptionGraphicsItem *option, QWidget *widget);
看到区别了吗?这很微妙,但您错过了const
如果您改为使用MyCity::paint
关键字来写override
,则像这样:
void paint(QPainter *painter, QStyleOptionGraphicsItem *option, QWidget *widget) override;
然后您将得到编译器错误
错误:“虚拟无效MyCity :: paint(...)”标记为“覆盖”,但没有覆盖