Program sets button labels, etc. before window shows

I’m trying to create a program that lights up buttons (more like setting an image) using a linked list sequence. The problem is that my program makes all these member functions before the window shows.

Code:

#include <gtkmm.h>
#include "mywindow.h"
#include <iostream>
#include <time.h>

MyWindow::MyWindow(){
set_border_width(10);
button[0].set_label("1");
button[1].set_label("2");
button[2].set_label("3");
button[3].set_label("4");

    for(int i = 0; i<4; i++){
    grid.attach(button[i], i, 1, 1,1);
    }
add(grid);
grid.show();
grid.show_all_children();
usleep(10);
srand(time(NULL));
computerlist.insert(computerlist.getSize(), rand()%4);
for(int j = 0; j<computerlist.getSize(); j++){
            std::cout<<computerlist.get(j)<<std::endl;
            }
for(int value = 0; value<computerlist.getSize(); value++){
    int timedel = time(0) + 2;
    if(computerlist.get(value) == 0){
        button[0].set_label("0);
        while(time(0)< timedel);
        button[0].set_label("1");
        }
    else if (computerlist.get(value) == 1){
        button[1].set_label("1");
        while(time(0)< timedel);
        button[1].set_label("2");
        }
    else if (computerlist.get(value) == 2){
        button[2].set_label("2");
        while(time(0)< timedel);
        button[2].set_label("3");
        }
    else if (computerlist.get(value) == 3){
        button[3].set_label("3");
        while(time(0)< timedel);
        button[3].set_label("4");
        }

}
    for(int i = 0; i<4; i++){
    button[i].signal_button_press_event().connect(sigc::bind<int>(sigc::mem_fun(*this, &MyWindow::on_button_press_event), i));
    button[i].signal_clicked().connect(sigc::bind<int>(sigc::mem_fun(*this, &MyWindow::on_button_clicked), i));
    }
};

I expect to see the buttons change numbers here. But what happens is the buttons change before the window shows, so I can only see that changed buttons already.

This is mywindow.h:

#ifndef __MYWINDOW__
#define __MYWINDOW__
#include <iostream>
#include <gtkmm.h>
#include "kellylist.h"
class butones: public Gtk::Button{
public:
butones(){
    this->set_events(Gdk:: BUTTON_PRESS_MASK);

}
};
class MyWindow: public Gtk::Window {
public:
    MyWindow();
    ~MyWindow();
    butones button[4];
    List<int> computerlist;
    List<int> playerlist;
    virtual bool on_button_press_event(GdkEventButton* event, int i);
protected:
    void on_button_clicked(int i);
    Gtk::Grid grid;
};

#endif

and this is where I put my linked list(for trial purposes):

#ifndef WALALANG
#define WALALANG
#include <iostream>
#include <cstdlib>
#include <string>

template <typename T>
class Node{
private:
    T data;
    Node<T>* prev;
    Node<T>* next;
public:
    Node<T>* getNext(){
        return next;
    }
    Node<T>* getPrev(){
        return prev;
    }
    void setPrev(Node<T>* prev){
        this->prev = prev;
    }
    void setNext(Node<T>* next){
        this->next = next;
    }
    T getData(){
        return data;
    }
    void setData(T dat){
        this->data = dat;
    }
    Node(){
        prev = 0;
        next = 0;
    }
};

template <typename T>
class List{
private:
    Node <T>* start;
    Node <T>* end ;
    int size;
public:
    void insert(int index, T data);
    void remove(int index);
    T get(int index);
    void clear();
    void set(int index, T data);
    int getSize(){
        return size;
    }
    bool isEmpty();
    bool Contains(T data);
    List() {
        start = 0;
        end = 0;
        size = 0;
    }
};


template <typename T>
void List<T>::insert(int index, T data){
Node <T>* m = this->start;
Node <T>*n = new Node <T>;
n->setData(data);
if (index> this->getSize()){
    std::cout<<"INVALID INDEX!"<<std::endl;
}
else{
    for (int i = 0; i<index; i++){
        m = m->getNext();
    }
    Node <T>* l;
    if (m)
        l = m->getPrev();
    else
        l = this->end;
    n->setPrev(l);
    n->setNext(m);
    if (l)
        l->setNext(n);
    else
        this->start = n;
    if (m)
        m->setPrev(n);
    else
        this->end = n;
    size++;
    }
}
template <typename T>
void List<T>::remove(int index){
Node <T>* n = this->start;
if (index>=this->size)
    std::cout<<"INVALID INDEX!n";
else{
    for(int i = 0; i<index; i++){
        n = n->getNext();
    }

    Node <T>* m = n->getNext();
    Node <T>* l = n->getPrev();
    if(l)
    l->setNext(m);
    else
    this->start = m;
    if(m)
    m->setPrev(l);
    else
    this->end = l;
    size--;
    delete n;
}
}
template <typename T>
bool List<T>::isEmpty(){
if (this->size == 0)
    return 1;
else
    return 0;
}
template <typename T>
T List<T>::get(int index){
if (index > size)exit(1);
Node<T>* temp = this->start;
for(int i = 0; i<index; i++){
    temp = temp->getNext();
}
return temp->getData();
}

template <typename T>
void List<T>::set(int index, T data1){
if (index > this->size)
    std::cout<<"Invalid index! ";
else{
    Node<T>* n = this->start;
    for(int i =0; i<index; i++){
        n = n->getNext();
    }
    n->setData(data1);
}
}
template <typename T>
void List<T>::clear(){
Node <T>*n = this->start;
for(int i = 0; i<size; i++){
    Node<T>* m = n;
    n = m->getNext();
    delete n;
}
this->size = 0;
}


template <typename T>
bool List<T>::Contains(T data){
Node<T>*n = this->start;
for(int i = 0; i<size; i++){
    if(!data.compare(n->getData()))
    return 1;
    n = n->getNext();
}
return 0;
}

#endif // WALALANG


Source: oop

Leave a Reply