Qtdesigner
Qtdesigner

Reputation: 135

Problem in constructor?

Hi I have problem in code that the following code retuning the error that

no matching function for call to QpushButton::QPushButton(QString*&,QWidget*&)
and

no matching function for call to QPainter::drawText(const QPointF&, const QString&)

and the code is

MainWindow::MainWindow(QWidget *parent) :
    QPushButton(parent)
{
    //ui->setupUi(this);
    //connect(this,SIGNAL(clicked()),this,SLOT(newWindow()));
}


MainWindow::MainWindow(QString *str,QWidget *parent) :
    QPushButton(str,parent)
{
    //ui->setupUi(this);
    text_str=str;
    connect(this,SIGNAL(clicked()),this,SLOT(newWindow()));
}

MainWindow::~MainWindow()
{
    //delete ui;
}

void MainWindow::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    QPen pen(Qt::black);
    pen.setWidth(6);
    painter.setPen(pen);

    static const QPointF points[6] = {
        QPointF(300.0, 160.0),//Top Line
        QPointF(220.0, 160.0),//
        //QPointF(300.0, 180.0),
        QPointF(195.0, 210.0),
        QPointF(220.0, 260.0),//
        QPointF(300.0, 260.0),//Bottom Line
        QPointF(325.0, 210.0)
    };

    painter.drawPolygon(points, 6);

    QPainter painter1(this);
    QPen pen1(Qt::green);
    painter1.setPen(pen1);
    QLinearGradient grad1(300, 160, 325, 260);
    QBrush brush1(grad1);
    grad1.setColorAt(1.0, Qt::white);
    painter1.setBrush(brush1);
    QFont font("Times", 12);
    painter1.setFont(font);
    QPoint point1 = QPoint( 240, 225);
    painter1.drawText( point1, text_str );
}

void MainWindow::newWindow()
{
    FrameWindow *frm=new FrameWindow(this);
    frm->show();
}

and the mainnWindow class is called by the following code

NewWindow::NewWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::NewWindow) { ui->setupUi(this); w1 = new MainWindow("Hello",this); w1->show(); w6->show(); }

NewWindow::~NewWindow() { delete ui; }

Please help me. Advance Thanks for your help.

Upvotes: 0

Views: 940

Answers (2)

Abhijith
Abhijith

Reputation: 2632

1) As already pointed out QPushButton has no constructor that takes QString * & QWidget * as arguments.I think you maybe be confused between passing by reference and passing a pointer.

2) The function signature for DrawText is

void QPainter::drawText ( const QPoint & position, const QString & text )

whereas text_str seems to be a pointer to QString. So use,

painter1.drawText( point1, *text_str );

Upvotes: 0

Steffen
Steffen

Reputation: 2948

  1. QPushButton's constructor takes a QString reference not a pointer, see http://doc.trolltech.com/4.7.1/qpushbutton.html#QPushButton-2 So you should change your constructor MainWindow::MainWindow(QString *str,QWidget *parent) to MainWindow::MainWindow(const QString &str,QWidget *parent) or similar.
  2. The drawText() error I don't quite get, because the signature seems ok. It is a bit unclear, what type text_str is. If that is a pointer to QString it would also not work, but the error message should be slightly different. (Also the point given in your only drawText call is not QPointF but QPoint, so I think there is something inconsistent between the errors and the code you posted)

Upvotes: 2

Related Questions