Hello, such a problem:

/home/u0807/QtApplication_2/main.cpp:83: ошибка: no matching function for call to 'QGraphicsTextItem::QGraphicsTextItem(int, myClass*)' itemText[1] = new QGraphicsTextItem(0, this); ^ 
 #include <QApplication> #include <QtCore> #include <QtGui> #include <QString> #include <QInputDialog> #include <QGraphicsScene> #include <QGraphicsItem> #include <QGraphicsView> #include <QPushButton> #include <QGridLayout> #include <QMessageBox> #include <QGraphicsEllipseItem> #include <QGraphicsTextItem> ..... for (int i = 0; i < 4; i++) { item[i] = new QGraphicsEllipseItem(0, this); item[i]->setRect(QRectF(-90, -35 + (i * 16), 15, 15)); item[i]->setBrush(QBrush(Qt::yellow)); item[i]->setAcceptHoverEvents(true); item[i]->setToolTip("10.31." + QString::number(i + 3) + ".130"); } itemText[1] = new QGraphicsTextItem(0, this); 

Tell me please.

  • See what the QGraphicsTextItem class has constructors and compare with yours. - Pavel Parshin

3 answers 3

The compiler tells you that the QGraphicsTextItem class has no QGraphicsTextItem view constructor QGraphicsTextItem(int, myClass*) , or it cannot implicitly QGraphicsTextItem(int, myClass*) passed parameters to the arguments of any of the available constructors.

  • The main joke is that I wrote this program in NetBeans, and there everything compiled fine, and now I am trying to compile it in QtCreator. - fredwriter
  • one
    @fredwriter Errors are not generated by the development environment, but by the compiler. So see what makes the compilers different. - Cerbo
  • @fredwriter Most likely this is where you made a mistake, because the first parameter in the appropriate QGraphicsTextItem constructor should be QString , and I do not believe that there will be at least one C ++ compiler in nature that implicitly converts the integer to QString . - Cerbo
  • I had a g ++ compiler, and now g ++. I wrote this program on NetBeans long ago. - fredwriter
  • itemText[1] = new QGraphicsTextItem(0, this); itemText[1]->setPlainText(Ss[0][4]); First we create and then initialize SetPlainTex - fredwriter

Try

 itemText[1] = new QGraphicsTextItem(QString::number(0), this); 

If you need exactly zero there, or just

 itemText[1] = new QGraphicsTextItem(this); 

This class in the documentation ( http://doc.qt.io/qt-5/qgraphicstextitem.html#QGraphicsTextItem-1 ) has two constructors, one of them in front of the parent accepts a string, and the second accepts only a pointer to the parent.

  • I have already tried it, this is my case, the context is not the same. In my case, it is executed in the context of the slot implementation for the class myClass described as follows: class myClass : public QGraphicsScene { Q_OBJECT public: myClass(QObject *parent = 0); QTimer *timer; InputDialog* pInputDialog = NULL; QDialog* D = NULL; QGraphicsEllipseItem* item[4]; public slots: void timer_overflow(); void btnClicked_Add(); void btnClicked_Del(); void slotFinished(); private: }; class myClass : public QGraphicsScene { Q_OBJECT public: myClass(QObject *parent = 0); QTimer *timer; InputDialog* pInputDialog = NULL; QDialog* D = NULL; QGraphicsEllipseItem* item[4]; public slots: void timer_overflow(); void btnClicked_Add(); void btnClicked_Del(); void slotFinished(); private: }; - fredwriter
  • That is, the myClass class is of QGraphicsScene type, but I need the QGraphicsItem idea. But the parent must be the scene. I can not understand, this code worked for me in NetBeans. - fredwriter
  • It is difficult to say exactly what behavior you need from this part of the code and the class as a whole, but if MyClass also inherits QGraphicsItem, then this code is compiled. But it seems to me that this is not the best solution. Perhaps a problem in the architecture of the project site. - Shadasviar
  • QGraphicsItem is the base class for the elements of the scene, but the samasacene cannot be the parent of the elements, it only places them in itself. It would be logical to create a new item in the method and add it to this by the addItem method, again, depending on the required behavior - Shadasviar
 for (int i = 0; i < 4; i++) { item[i] = new QGraphicsEllipseItem(0); item[i]->setRect(QRectF(-90, -35 + (i * 16), 15, 15)); item[i]->setBrush(QBrush(Qt::yellow)); item[i]->setAcceptHoverEvents(true); item[i]->setToolTip("10.31." + QString::number(i + 3) + ".130"); myClass::addItem(item[i]); } itemText[1] = new QGraphicsTextItem(0); myClass::addItem(itemText[1]);