Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ gui help

Options
  • 27-03-2009 4:59pm
    #1
    Registered Users Posts: 1,366 ✭✭✭


    Hey,

    I'm porting some code over from matlab to c++

    Been 6 years since I did any c++. The main code's working but I need some sort of a gui to display a 100x100 array.

    I'd like a 2D image of the array. If array[j] = 1, the pixel should be black, or if array[j] = 0, the pixel in the 100x100 image should be white.

    These values change all the time so the image needs to be continually updated.

    It's easy in matlab, but I've no idea how to do this in c++

    Any ideas?

    M


Comments

  • Closed Accounts Posts: 218 ✭✭Void


    Sry if this isn't the answer you want but....

    Don't use C++ to do GUI stuff. Much easier to do it in C# or java or whatever. If you are gonna start on about "c++ performance", then put your maths intensive stuff in a DLL and link to it from C# GUI. Very very easy to do using CLR (can even embed c++ class inside c# app).

    Oh, and if you don't want to use Win32 at all, then try GUI in Java? As last resort, use some gui library like tcl inside your C++ app. Whatever you do...... do NOT even think of using MFC (will scar you for life).


  • Closed Accounts Posts: 218 ✭✭Void


    However upon reflection it seems that you want a graphics library, not a full gui system. Try this: http://www.libsdl.org/download-1.2.php, or you could use OpenGL or DirectX as well.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Void wrote: »
    Sry if this isn't the answer you want but....

    Don't use C++ to do GUI stuff. Much easier to do it in C# or java or whatever. If you are gonna start on about "c++ performance", then put your maths intensive stuff in a DLL and link to it from C# GUI. Very very easy to do using CLR (can even embed c++ class inside c# app).

    Oh, and if you don't want to use Win32 at all, then try GUI in Java? As last resort, use some gui library like tcl inside your C++ app. Whatever you do...... do NOT even think of using MFC (will scar you for life).
    Not necessarily. QT is a great GUI tool kit for C++ and is quick, very simple and feature packed and cross platform compatible. However, for this - it may be an overkill.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    +1 for QT.

    Hardly overkill. To draw you can use the QImage class for one.
    //customwidget.h
    #ifndef CUSTOMWIDGET_H
    #define CUSTOMWIDGET_H
    
    #include <QWidget>
    
    class CustomWidget : public QWidget
    {
    public:
        CustomWidget();
        void paintEvent(QPaintEvent *event);
    };
    
    #endif // CUSTOMWIDGET_H 
    
    #include "customwidget.h"
    //customwidget.cpp
    #include "Qpainter.h"
    
    CustomWidget::CustomWidget()
    {
    }
    
    void CustomWidget::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
    // Draw a 3 by 3 array.
     QImage image(3, 3, QImage::Format_RGB32);
     QRgb colourA,colourB,colourZ;
    
    //Use these colours
     colourA = qRgb(189, 149, 39); // 0xffbd9527
     colourB = qRgb(122, 163, 39); // 0xff7aa327
     colourZ = qRgb(237, 187, 51); // 0xffedba31
    
    // Set the colour of each pixel. Put in a loop for your case
     image.setPixel(0, 0, colourZ);
     image.setPixel(0, 1, colourZ);
     image.setPixel(0, 2, colourB);
     image.setPixel(1, 0, colourA);
     image.setPixel(1, 1, colourB);
     image.setPixel(1, 2, colourA);
     image.setPixel(2, 0, colourB);
     image.setPixel(2, 1, colourB);
     image.setPixel(2, 2, colourA);
    
    // Scale up the image to a viewable 400 x 400 size.
     image = image.scaled(400,400,Qt::KeepAspectRatio,Qt::FastTransformation);
        painter.drawImage( QPoint(10,10),image);
    
    }
    
    //Main.Cpp
    
    #include <QtGui/QApplication>
    #include "customwidget.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        CustomWidget w;
        w.show();
        return a.exec();
    }
    


  • Registered Users Posts: 981 ✭✭✭fasty


    An alternative would be SDL. You can easily set pixels on a surface using it.


  • Advertisement
Advertisement