0

カスタムメイドのボタンの配列があります:

Button buttons[5]

ここで、この配列の 2 つの要素、たとえばボタン [1] とボタン [2] を交換したいと思います。どうすればいいですか?次のように述べるだけではうまくいきません:

Button help = buttons[1];
buttons[1] = buttons[2];
buttons[2] = help;

誰でもこれで私を助けることができますか?

ポインター配列を使用して解決しました:

Button *pntArray[5];
Button *help;
pntArray[0]=&buttons[0];
pntArray[1]=&buttons[1];

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

1 に答える 1

1

QObject基本クラスでは、代入演算子またはコピーコンストラクターは許可されていません。これらを手動で作成していない限り(通常は賢明ではありません)、ヒープ上でインスタンスを宣言し、代わりに配列内のポインターを使用してください。

//  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] );
于 2012-07-10T12:37:15.153 に答える