user3676560
user3676560

Reputation: 151

qt c++ qpainter with QVector

So I have an QVector called xarray

QVector< QString > xarray;

Now I want to draw the array

void MainWindow::paintEvent( QPaintEvent* e )
{
    QPainter painter(this);

    for(int i = 0; i < 5; i++)
    {
        qDebug() << "\r\narr : " <<QList<QString>::fromVector(xarray);
        painter.drawEllipse(X, 10, 100, 100);
    }
}

How can I give the array to my x comp ?

I tried

painter.drawEllipse(xarray[i], 10, 100, 100);

But there is no function for call to QPainter.

Upvotes: 0

Views: 337

Answers (2)

Daniel Dinu
Daniel Dinu

Reputation: 1791

I think you're looking for QString::toInt().

You have an array of QStrings but you need to pass an int to drawEllipse(), the overload that takes ints. You will need to convert your strings to integers.

A quick edit to your code would turn it into:

void MainWindow::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);

    for(int i = 0; i < 5; i++)
    {
       qDebug() << "\r\narr : " <<QList<QString>::fromVector(xarray);
       painter.drawEllipse(xarray[i].toInt(), 10, 100, 100);
    }
 }

I didn't actually compile that, hope it works and you get the idea.

Upvotes: 1

Alexander Stepaniuk
Alexander Stepaniuk

Reputation: 6408

A naive implementation could look like:

void MainWindow::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);

    for(int i = 0; i < 5; i++)
    {
        painter.drawEllipse(xarray[i].toFloat(), 10.0, 100.0, 100.0);
    }
}

You need to convert QString value into float value before use it as an argument for drawEllipse

But in real code I strongly recommend to make sure that values in xarray can be converted to float before use them.

Upvotes: 2

Related Questions