Nominate our 2022 Qt Champions!

QImage question

  • Hello,
    I am not going to ask something I couldnt handle this time. My code works ( Yee Haw ) and it produces what I desire and it was, taking this image: into memory to display this: Adding an interactive areas per specific pixels. Right now my code just checks QColor values like red(), blue() and green() if they are 0 then add the custom item. But I want to ask should I care about black(), cmyk() magentas and alpha? Also QColor has a method getRgb(int*, int*,int*, int*); which sets a custom array of 4 ints to colors. Is it a good idea to use that method, assuming the array is some private variable? Or the comparison of is sufficient. For now I dont see any unacceptable speed. I am not making a realtime action games so its affordable. Here is a snap of my code Id like to hear some recommendations if any:
    MainWindow::MainWindow(QApplication* qapp) {

    QString wavFile(QCoreApplication::applicationDirPath());
    QString s1(wavFile);
    qDebug() << "Debugging filepaths: " << s1 << "\n";
    player =  new QMediaPlayer();

    // player->setMedia(QUrl::fromLocalFile("/home/ilian/QT5/build-MathForFun-Desktop_Qt_5_3_GCC_32bit-Debug/blop.wav"));


    // QPixmap pim("test.png");
    QImage image;

    QPixmap pim("test3.png");
    image = pim.toImage();
    QRgb rgb ;
    QRgb special = qRgb(255,0,255);
    /* iterate trough image */
    int i, j;
    ActionItem *tmpItem;
    for(i=0; i < 480; i++) {
        for (j=0; j < 640; j++) {
            rgb = qRgb(i,j, 0);
            /* Width is for horizontal
             * height for vertical */
            if ( i % 2 == 0 && j % 2 == 0) {
                //image.setPixel(j, i, special);
            } else {
                /* deprecate */
                //image.setPixel(j, i, rgb);
            QColor colVal(image.pixel(j,i));
           // int cols[4];
           // colVal.getRgb(&cols[0], &cols[1], &cols[2], &cols[3]);
          //  qDebug() << <<":"<<<<":"<<<<"\n";
            if ( && == 0 && == 0) {
                actionItems.append(new ActionItem(j,i, 10,10, "TMPITEM", 1, this));

    /* [!] important:

    • now got the color vals, must find

    • specific value and update the coords

    • the xy of the pixel will be the center of a elipse /
      qDebug() << "RGB: " << << << << "\n";
      pim = QPixmap::fromImage(image);

      __qapp = qapp;
      __scene = new QGraphicsScene;
      __view = new QGraphicsView(__scene);
      __scene->addLine(QLineF(0,0,640,480)); //testline
      __scene->setSceneRect(QRectF(0,0, 640, 480));

      int containerVar=0;
      /*[1] remove comment after test

      • for (i=0; i < 940; i+= 20, j) {
        for ( j=0; j < 580; j+= 20) {
        actionItems.append(new ActionItem(i, j, 20,20,
        "Item", containerVar,
        this)); //new - sound effect

      for (i=0; i < actionItems.size(); i++) {



  • Moderators

    I'm not gonna be much help because I'm not sure what's the question, but let me comment on your code.

    It's a good idea to clean up your code before posting. anyone getting here and looking at it will be very discouraged to help you when he sees it like that.

    Half of the code is commented out, which makes it very hard to read. Is it relevant code? Does it contain something I should look at or can I skip it? Around line 31 you have an if/else that contains no code. How am I suppose to understand your intention here?

    All in all I have no idea what that code does. Plays an audio file, does something with RGB values, adds some lines and actions to some scene. You should take a look at the concept of "Single Responsibility Principle": Let one function do one thing.

    Also, identifier names starting with an underscore and double underscore are reserved by the standard for the compiler and STL implementations. You are not suppose to use names like that in your code.

  • Yeah. Sorry. I just wrote it on a blink so didn`t have time to polish it. The double underscore is my bad C habbit...

  • Moderators

    I believe that rule got to C++ from C, so it's a bad habit even there ;)

Log in to reply