Question : what is the problem in this code

what is the problem in this code :
I want it draw many rectangles with different high and color

when I compile it I receive this error:
1- 'string' was not declared in this scope
2- error: expected `;' before 's'
3- error: 's' was not declared in this scope
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
string s[3]={"Qt::black","Qt::red","Qt::blue"};
    int a[3] ={60,40,120};
    for(int i=0 ; i<3 ;i++){
        for(int x=6 ;x<18;x=x+6){

            QRectF rect1(x,0.0,0.0,-a[i]); //
            line.setPen(QPen(s[i],8,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin));
            line.drawRect(rect1);
        }
    }

Answer : what is the problem in this code

Hi obad62,

IMO you either use the wrong string class or don't use it correctly.

If you want to use 'string' from STL you'll have to:
- include 'string' header, i.e.:
#include <string>
- prefix the 'string' with the 'std' namespace, i.e.:
std::string s[3]= ...

Or maybe you want to use a QString instead.

BTW, I didn't find a 'QPen' constructor which takes a string as first parameter.

So, IMO the attempt how you do it is wrong - instead of a string you'll either have to use an array of 'QColor's instead of strings or generate 'QColor's by passing strings from the array - I think the first one is easier.

To do so I changed the code a bit and attached it here - I hope it's mostly correct since I couldn't test it here since I have no Qt.

Hope that helps,

ZOPPO
1:
2:
3:
4:
5:
6:
7:
8:
9:
QColor s[3]={Qt::black,Qt::red,Qt::blue};
int a[3] ={60,40,120};
for(int i=0 ; i<3 ;i++){
 for(int x=6 ;x<18;x=x+6){
  QRectF rect1(x,0.0,0.0,-a[i]); //
  line.setPen(QPen(s[i],8,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin));
  line.drawRect(rect1);
 }
}
Random Solutions  
 
programming4us programming4us