title: How do I use QPainter in only one cpp file?
body: <p>here's my code:</p>
  
  <pre><code>#include&lt;QApplication&gt;
  #include&lt;QWidget&gt;
  #include&lt;QPaintEvent&gt;
  #include&lt;QPainter&gt;
  
  int main(int argc,char**argv)
  {
  QApplication app(argc,argv);
  QWidget*wid=new QWidget;
  wid-&gt;setWindowTitle("sb");
  
  QPainter paint;
  paint.setWindow(0,0,900,900);
  QRectF border(3*45-20,25,670,850);
  QRectF inter(3*45,45,630,810);
  paint.setPen(Qt::NoPen);
  paint.setBrush(QBrush(Qt::darkMagenta,Qt::SolidPattern));
  paint.drawRect(border);
  paint.setBrush(QBrush(Qt::gray,Qt::SolidPattern));
  paint.drawRect(inter);//
  paint.setPen(QPen(Qt::darkGray,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
  paint.setPen(Qt::NoPen);
  
  paint.beginNativePainting();
  wid-&gt;show();
  return app.exec();
  }
  </code></pre>
  
  <p>after qmake -project ,qmake ,make ,then it goes wrong as follows:</p>
  
  <pre><code>kl@kl-Latitude-D630:~/QTproj/t1$ ./t1 
  QPainter::setWindow: Painter not active
  QPainter::setPen: Painter not active
  QPainter::setBrush: Painter not active
  QPainter::drawRects: Painter not active
  QPainter::setBrush: Painter not active
  QPainter::drawRects: Painter not active
  QPainter::setPen: Painter not active
  QPainter::setPen: Painter not active
  QPainter::beginNativePainting: Painter not active
  </code></pre>
  
  <p>Well ~~ How to active the QPainter only in one cpp file(I don't wanna use separate files such as paint.h ,paint.cpp and main.cpp . I know I can make it work if I create a class inherit the QWidget, but I wanna have a try how to make it work in one file)?? thanks a lot ~;-)</p>