Q:

Swapping Buttons in array

I have an array of my custom made buttons: 
Button buttons[5]

Now I want to swap two elements of this array, for example buttons[1] and buttons[2]. How do I do this? Just stating the following, doesnt work:
Button help = buttons[1];
buttons[1] = buttons[2];
buttons[2] = help;

Can anybody help me with this?
I have solved using a pointer array:
Button *pntArray[5];
Button *help;
pntArray[0]=&buttons[0];
pntArray[1]=&buttons[1];

help=pntArray[0];
pntArray[0]=pntArray[1];
pntArray[1]=help;

A:

QObject base class does not allow the assignment operator or the copy constructor.  Unless you have manually created these (which is usually unwise), declare your instances on the heap and use pointers in the array instead.
//  Instantiate the buttons however you like, if you were just creating them
//  on the stack before, a default initialisation should suffice.  Though
//  normally in Qt you would at least pass the 'owning' widget as the parent
//  so you don't need to worry about deleting the buttons.
QVector<Button*> buttons(5);
for ( Button* button : buttons ) {  // C++11 only!
    button = new Button();
}

//  Then whenever you need to swap two entries:
std::swap( buttons[1], buttons[2] );

