Basic GUI Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Laurent Berger
Earned some good credits
Earned some good credits
Posts: 138
Joined: Tue May 20, 2008 1:03 pm

Basic GUI

Post by Laurent Berger »

Hi,

I'm looking for basic GUi for noobs. Something like this

Code: Select all

void main (void)
{
double x=wxGetValue("Real number please:");
wxString s=wxGetString("Your name please:);
}
wxGetValue or wxGetString open a dialog box and when user press return key dialog is closed. No need of event loop. Is possible with wxWidgets?
L.B.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Basic GUI

Post by doublemax »

This is the simplest you can do:

Code: Select all

#include "wx/wx.h"
#include <wx/numdlg.h> 

class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {
      long number = wxGetNumberFromUser("enter number", "your prompt here", "your caption here", 1);
      wxString s = wxGetTextFromUser("enter string", "your caption here", "" );

      wxLogMessage("your number was %d and the string was '%s'", number, s );

      return false;
    }
};

IMPLEMENT_APP(MyApp)
Use the source, Luke!
Laurent Berger
Earned some good credits
Earned some good credits
Posts: 138
Joined: Tue May 20, 2008 1:03 pm

Re: Basic GUI

Post by Laurent Berger »

Thanks
I need main entry point. With your answer and viewtopic.php?t=43513

Code: Select all

#include "wx/wx.h"
#include <wx/numdlg.h>
#include<iostream>
#include<string>

class GetNumberApp : public wxApp
{
public:
    int number;
    virtual bool OnInit()
    {
        number = wxGetNumberFromUser("enter number", "your prompt here", "your caption here", 1);

        return false;
    }
};

class GetTextApp : public wxApp
{
public:
    wxString texte;
    virtual bool OnInit()
    {
        texte = wxGetTextFromUser("enter number", "your prompt here", "");

        return false;
    }

};

class GetFileNameApp : public wxApp
{
public:
    std::string fileName;
    virtual bool OnInit()
    {
        fileName = wxFileSelector("Choose a file to open");

        return false;
    }
};

std::string GetFileName()
{
    std::string x;
    GetFileNameApp* pApp = new GetFileNameApp();
    wxApp::SetInstance(pApp);
    wxEntryStart(0, NULL);
    wxTheApp->OnInit();

    x = pApp->fileName;
    // cleaning up...
    wxTheApp->OnExit();
    wxEntryCleanup();

    return x;
}

template<class T>T GetNumber()
{
    T x;
    GetNumberApp* pApp = new GetNumberApp();
    wxApp::SetInstance(pApp);
    wxEntryStart(0, NULL);
    wxTheApp->OnInit();

    x = pApp->number;
    // cleaning up...
    wxTheApp->OnExit();
    wxEntryCleanup();

    return x;
}

template<>double GetNumber<double>()
{
    double x;
    GetTextApp* pApp = new GetTextApp();
    wxApp::SetInstance(pApp);
    wxEntryStart(0, NULL);
    wxTheApp->OnInit();

    pApp->texte.ToCDouble(&x);
    // cleaning up...
    wxTheApp->OnExit();
    wxEntryCleanup();

    return x;
}

void main(int argc,char**argv)
{
    
    int l1 = GetNumber<int>();
    std::cout << "First number "<<l1 << "\n";
    double l2 = GetNumber<double>();
    std::cout << "Second number " << l2 << "\n";
    std::string s = GetFileName();
    std::cout << "File name " << s << "\n";
    // cleaning up...
}

Do you think I can use this way to write basic gui?
L.B.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Basic GUI

Post by doublemax »

This looks unnecessarily complicated. What exactly are you trying to do?
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Basic GUI

Post by ONEEYEMAN »

Hi,
On top of what doublemax said - you can't have 2 wxApp-derived classes in the project. What is the purpose?
Also, based on your responce and the code you wrote it seems you don't understand the notion of the GUI and wxWidgets in general.

I suggest your start with some basic C++ classes, then take some GUI programming classes and then move to some specific framework - i.e. MFC, wxWidgets, GTK, Qt, FLTK, etc.

Thank you.
Laurent Berger
Earned some good credits
Earned some good credits
Posts: 138
Joined: Tue May 20, 2008 1:03 pm

Re: Basic GUI

Post by Laurent Berger »

ONEEYEMAN wrote:Hi,
On top of what doublemax said - you can't have 2 wxApp-derived classes in the project. What is the purpose?
Also, based on your responce and the code you wrote it seems you don't understand the notion of the GUI and wxWidgets in general.

I suggest your start with some basic C++ classes, then take some GUI programming classes and then move to some specific framework - i.e. MFC, wxWidgets, GTK, Qt, FLTK, etc.

Thank you.
Why do you write such thing? it sounds like insults
L.B.
Laurent Berger
Earned some good credits
Earned some good credits
Posts: 138
Joined: Tue May 20, 2008 1:03 pm

Re: Basic GUI

Post by Laurent Berger »

doublemax wrote:This looks unnecessarily complicated. What exactly are you trying to do?
My students don't know gui loop and have to learn some basics in C++ in console mode.

Today cout and cin in a black screen with young people knowing only popup windows in instagram, facebook..it's difficult.
I want something really light to read data (int double, string and filename) in popup windows
I try nanogui, imgui : it is not for noobs
L.B.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Basic GUI

Post by ONEEYEMAN »

Hi,
I didn't want to insult you. If I did - I apologize.

Going back to your question - did you look at the console sample?

Thank you.
Laurent Berger
Earned some good credits
Earned some good credits
Posts: 138
Joined: Tue May 20, 2008 1:03 pm

Re: Basic GUI

Post by Laurent Berger »

ONEEYEMAN wrote:Hi,
I didn't want to insult you. If I did - I apologize.

Going back to your question - did you look at the console sample?

Thank you.
Ok close
Now you said "you can't have 2 wxApp-derived classes in the project" but when I run my program It works :

Code: Select all

   int l1 = GetNumber<int>(); // I can see a window here and press return closed
    std::cout << "First number "<<l1 << "\n";
    double l2 = GetNumber<double>();
    std::cout << "Second number " << l2 << "\n";
    std::string s = GetFileName();
    std::cout << "File name " << s << "\n";
Application video :http://perso.univ-lemans.fr/~berger/Afs ... 211646.mp4 Video made with https://github.com/LaurentBerger/wxFilmEcran
I tried wxConsole I cannot see any box to read data (with -d or -s)

My question is then how can I write a programm using in console mode to get a result same as my video?
L.B.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Basic GUI

Post by doublemax »

My question is then how can I write a programm using in console mode to get a result same as my video?
It sounds as if your solution already works?

This is the modified version of my earlier code:

Code: Select all

#include "wx/wx.h"
#include <wx/numdlg.h>
#include <iostream>

int main(int argc, char **argv)
{
    wxInitializer init(argc, argv);
    if ( !init.IsOk() ) {
        wxPrintf("Failed to initialize wxWidgets.\n");
        return 1;
    }

    wxApp::SetInstance( new wxApp() );

    long number = wxGetNumberFromUser("enter number", "your prompt here", "your caption here", 1);
    wxString s = wxGetTextFromUser("enter string", "your caption here", "" );

#if 0
    wxLogMessage("your number was %d and the string was '%s'", number, s );
#else
    std::cout << "number "<< number << "\n";
    std::cout << "string "<< s.ToAscii() << "\n";
#endif
    return 0;
}
This is quick-and-dirty, i don't even know if the wxApp construction is ok this way. But i tested it and it seems to work.
Use the source, Luke!
Laurent Berger
Earned some good credits
Earned some good credits
Posts: 138
Joined: Tue May 20, 2008 1:03 pm

Re: Basic GUI

Post by Laurent Berger »

Nice and only one wxApp as suggested by ONEEYEMAN
I tested with windows 10 and vs 2017. May be it's a good sample to add to wxWidgets repo

What's your platform?
L.B.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Basic GUI

Post by doublemax »

I'm using Windows 7 and VS2013.
Use the source, Luke!
Post Reply