Objects from text



  • Hello.
    First of all... I'm new to Qt and kind of c++ :|

    Php/html/js/css etc I know, from them I've learned programming, but the problem is, that I can't find a way to do this in C++:

    [code]
    $x="board_33";
    $ui->$x()->setText("Hi");
    [/code]

    In c++, I have this:
    [code]

        if(where==1&&where2==1){ui->board_11->setText(set);}
        if(where==1&&where2==2){ui->board_12->setText(set);}
        if(where==1&&where2==3){ui->board_13->setText(set);}
    
        if(where==2&&where2==1){ui->board_21->setText(set);}
        if(where==2&&where2==2){ui->board_22->setText(set);}
        if(where==2&&where2==3){ui->board_23->setText(set);}
    
        if(where==3&&where2==1){ui->board_31->setText(set);}
        if(where==3&&where2==2){ui->board_32->setText(set);}
        if(where==3&&where2==3){ui->board_33->setText(set);}
    

    [/code]

    Is there a way I could do something like this in Qt? :
    @
    for(i=1;...){
    for(j=1;....){
    settext="board_"+i+j;
    ui->settext->setText(set);
    }
    }
    @
    I don't want those if's and writing ui->board_33->setText(set); all of the time, so basically if there's a way to change board_33 in ui->board33->setText(set); and use it with for function and others?

    Thanks for replies :)

    I'm not 100% sure these things are called objects. I'm sorry if I'm mistaking.



  • [quote author="Fear" date="1359589488"]

    Is there a way I could do something like this in Qt? :

    [code]for(i=1;...){
    for(j=1;....){
    settext="board_"+i+j;
    ui->settext->setText(set);
    }
    }[/code]
    [/quote]

    Yes there is. But I doubt about how you implemented lines 3 and 4. You might want to use the "Container classes":http://qt-project.org/doc/qt-4.8/containers.html for that.



  • You might want to learn some C++ before or in parallel with Qt before you rush into production, the paradigm is slightly different compared to how things are done with php or even JS.

    Coming from web programming, C++ might turn out to be a little uphill, I had the fortune to do it the other way around - first C and C++ and then php/JS and it was a breeze.

    Wasn't really all that charmed by php anyway, never quite figured why the hell does php feel the need to call maps "arrays", why no overloads, why no templates, the crummy constructor syntax, why concatenate strings with . and fall back to -> for member access, which is the syntax for member access through pointers in C++. The bad design and terrible performance aside, php is still way easier for web programming than C++, so as terrible as php is, it still has its market thanks to the lack of proper competition.



  • Best would be to store pointers to your widgets in a container that provides access in the way you need it once (from the constructor), and just use those pointers from there on.



  • If you are looking for a way to find a QObject (or widget) by its name, you can use the following:

    @
    QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
    @

    The template specialization (class name between <>) must match the object class you're trying to reach (or be one of the base classes). findChild and findChildren methods search for children recursively.

    "findChild method documentation":http://qt-project.org/doc/qt-4.8/qobject.html#findChild



  • "This is a reply from another forum, seems about right what I need":http://clip2net.com/s/2Mea5

    utcenter, I like learning by making, by making tic tac toes I learned the basics and I'm moving further and further every line of code I make :)

    Learning different languages isn't hard when you know 1 or 2 already, in this case it's php and js for me, because of that I have no problem with making anything in C++, besides finding the correct functions, operators etc...

    That's how I learned all the web programming on my own, just making stuff and finding everything I need to get some project done, now I have no problems with web programming.

    So far in making tic tac toe I had no problems, I'm just optimizing everything now and finding ways to get everything done easier, I'm currently making AI for it, but I didn't want to write all of those ifs, when I was 100% sure there was a way to do it with pointers :)

    Thanks for all your feedback, I will give everything a try!

    Best wishes, Dominykas.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.