Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. OpenGL painter->fillRect Question
QtWS25 Last Chance

OpenGL painter->fillRect Question

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cazador7907
    wrote on last edited by
    #1

    I've been working at learning to program with Qt and OpenGL. So far, I've been playing with overlays and painting images on the screen. This evening however, I was attempting to set up a small program where the User would click a button and some text would be written onto the screen.

    I started by creating a simple Widget class based on the QGLWidget base class. When the widget load's it displays a simple green background color. When a Users clicks a button it should write some text on the screen. The code to write to the screen comes in from "buttonClick()" which is wired to a SIGNAL/SLOT combination.

    When the text is written on the screen, the color disappears. I suspect that I'm either not refreshing something correctly or I'm not drawing something is the correct order. Either way, I'm not sure what is not happening. I've posted the code for the class below. Any help would be appreciated!

    @
    #include "glwidget.h"

    GLWidget::GLWidget(QGLWidget *parent) :
    QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
    {
    //Set the initial colors
    qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
    qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
    }

    GLWidget::~GLWidget()
    {
    //Do nothing
    }

    QSize GLWidget::minimumSizeHint() const
    {
    return QSize(50, 50);
    }

    QSize GLWidget::sizeHint() const
    {
    return QSize(400, 400);
    }

    void GLWidget::initializeGL()
    {
    qglClearColor(qtGreen.dark());

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_MULTISAMPLE);
    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    

    }

    void GLWidget::resizeGL(int width, int height)
    {
    int side = qMin(width, height);
    glViewport((width - side) / 2, (height - side) / 2, side, side);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    

    #ifdef QT_OPENGL_ES_1
    glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
    #else
    glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
    #endif
    glMatrixMode(GL_MODELVIEW);
    }

    void GLWidget::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    }

    void GLWidget::buttonClick()
    {
    QPainter painter(this);

    drawInstructions(&painter);
    
    painter.end();
    

    }

    void GLWidget::drawInstructions(QPainter *painter)
    {

    QString text = tr("Some instructions.");
    QFontMetrics metrics = QFontMetrics(font());
    int border = qMax(4, metrics.leading());
    
    QRect rect = metrics.boundingRect(  0, 0, width() - 2*border, int(height()*0.125),
                                        Qt::AlignCenter | Qt::TextWordWrap, text);
    
    painter->setRenderHint(QPainter::TextAntialiasing);
    
    painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border),
                     QColor(1, 0, 0, 255));
    painter->setPen(Qt::white);
    painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border),
                      QColor(1, 0, 0, 255));
    painter->drawText((width() - rect.width())/2, border,
                      rect.width(), rect.height(),
                      Qt::AlignCenter | Qt::TextWordWrap, text);
    

    }

    @

    Laurence -

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      You could use debugging in your member functions to see which ones are actually called or not and in what sequence.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        You are trying to mix OpenGL rendering with QPainter painting and you are also trying to paint on the widget outside of the paint event.

        If you want to ue QPainter in addition to OpenGL - ie not using OpenGL to draw all of yoru scene then I suggest you have a read of the the "OpenGL overpainting example":http://doc.qt.nokia.com/latest/opengl-overpainting.html.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved