Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ gui help

  • 27-03-2009 04:59PM
    #1
    Registered Users, Registered Users 2 Posts: 1,368 ✭✭✭


    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, Registered Users 2 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, Registered Users 2, Paid Member Posts: 2,427 ✭✭✭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, Registered Users 2 Posts: 981 ✭✭✭fasty


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


  • Advertisement
Advertisement