Results 1 to 3 of 3

Thread: QGraphicsScene addText() - Set font colour?

  1. #1
    Join Date
    Aug 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsScene addText() - Set font colour?

    I am using a QGraphicsScene to implement a mobile Splash Screen and I would like to use a dark background. The issue is that I cannot seem to change the font to anything but black.

    This is my main():

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. MediaLibrary mLib;
    6.  
    7. // Splash Screen
    8.  
    9. QLinearGradient lGrad(QPointF(100, 100), QPointF(200, 200));
    10. lGrad.setColorAt(0, Qt::black);
    11. lGrad.setColorAt(1, Qt::darkBlue);
    12. scene.setBackgroundBrush(lGrad);
    13. scene.addText("Media Library", QFont("Times", 30));
    14.  
    15. QGraphicsView view(&scene);
    16. view.show();
    17.  
    18. QTimer::singleShot(2000, &view, SLOT(close()));
    19. QTimer::singleShot(2000, &mLib, SLOT(show()));
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    What would be the best practise for changing the font colour?

    Thank you

  2. #2
    Join Date
    Aug 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene addText() - Set font colour?

    I solved this by creating a dedicated widget and reimplementing paintEvent().

    Incase anyone is seeking a similar solution (I did not find anything on Google relating to changing QGraphicsScene font colour), this is what I did:

    splashscreen.h
    Qt Code:
    1. #ifndef SPLASHSCREEN_H
    2. #define SPLASHSCREEN_H
    3.  
    4. #include <QWidget>
    5.  
    6. #include <QPainter>
    7.  
    8. class SplashScreen : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit SplashScreen(QWidget *parent = 0);
    13.  
    14. protected:
    15. void paintEvent(QPaintEvent *pEvent); // Reimplement paintEvent() to allow access to QPainter
    16.  
    17. private:
    18. // UI Elements
    19. QPainter *painter;
    20. };
    21.  
    22. #endif // SPLASHSCREEN_H
    To copy to clipboard, switch view to plain text mode 

    splashscreen.cpp
    Qt Code:
    1. #include "splashscreen.h"
    2.  
    3. SplashScreen::SplashScreen(QWidget *parent) : QWidget(parent)
    4. {
    5. // Nothing required here
    6. }
    7.  
    8. // Reimplemented paintEvent()
    9. void SplashScreen::paintEvent(QPaintEvent *pEvent)
    10. {
    11. painter = new QPainter(this); // Initialise painter
    12.  
    13. QLinearGradient lGrad(QPointF(500, 350), QPointF(700, 400)); // Set a gradient background
    14. lGrad.setColorAt(0, Qt::black);
    15. lGrad.setColorAt(1, Qt::darkBlue);
    16. painter->fillRect(0, 0, 800, 440, lGrad);
    17.  
    18. painter->setPen(Qt::white); // Add text
    19. painter->drawText(320, 150, "Media Library v0.1");
    20. painter->drawText(20, 400, "Gavin Harper (F8827)");
    21. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android Maemo/MeeGo

    Smile Re: QGraphicsScene addText() - Set font colour?

    This way it works
    (just change to your variables you need):


    QGraphicsSimpleTextItem * simpleTextItem1 = graphicsScene->addSimpleText(QString("my text number%1").arg(i+1));
    simpleTextItem1->setBrush(QColor(255, 255, 0, 127));
    //you get yellow semitransparent (text) colour or whatever the colour you want

Similar Threads

  1. AddText to QRect on QGraphicsView
    By mohini in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 22:43
  2. Right justify QPainterPath addText
    By GimpMaster in forum Qt Programming
    Replies: 5
    Last Post: 20th January 2011, 21:18
  3. Font Height and width based on font size
    By Ghufran in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2010, 09:02
  4. Change Font of QListWidget to Monospace Font
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 19:23
  5. font incorrectly show - font break.
    By sgh in forum Qt Programming
    Replies: 9
    Last Post: 30th May 2008, 03:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.