2017-01-28 15 views
0

這是代碼的定位的for迴路和給尺寸,以22個按鈕:使過按鈕的變化發送到QSignalMapper Qt中

for(int i=0; i<texts.size(); ++i) 
     { 
     QPushButton* button = new QPushButton(texts[i]); 

     connect(button, SIGNAL(clicked(bool)), 
       signalMapper, SLOT(map())); 
     button -> setFixedSize(50,30); 

     signalMapper -> setMapping(button, texts[i]); 
     gridLayout -> addWidget(button, i/5, i%5); 
     } 

輸出的圖像作爲以下內容:

enter image description here

我想操縱sizepositioncolor每個按鈕 「手動」 的font。我想我需要以某種方式收回signalMapper的按鈕,這樣我才能夠對他們做這些工作。你同意嗎?
請做那些作品的最簡單和最直接的方法是什麼? PS:我只在C++代碼中編寫應用程序,並沒有使用設計師

+0

你爲什麼不['setStyleSheet()'](https://doc.qt.io/qt-5/qwidget.html#styleSheet-prop)/在將按鈕添加到佈局之前,按鈕上的['setPalette()'](https://doc.qt.io/qt-5/qwidget.html#palette-prop)?也許有另一個用於styleSheets/Palettes的數組,並將其與你的'texts'數組一起用於循環中。 – Mike

回答

0

你可以得到映射函數的每個按鈕:

QPushButton* button = static_cast<QPushButton*>(this->siganlMapper.mapping("1")); 
button->setStyleSheet("color: red"); 

這將與1號更改按鈕的文字顏色爲紅色

0

您可以創建一個QList<QPuchButton>動態存儲所有的您的按鈕進入。在創建時執行此操作。它將連接起來是這樣的:

QList<QPushButton*> calcButtons;//QList for storing the buttons 
    for(int i=0; i<texts.size(); ++i) 
    { 
     QPushButton* button = new QPushButton(texts[i]); 
     calcButtons << button; 
     connect(button, SIGNAL(clicked(bool)), 
       signalMapper, SLOT(map())); 
     button -> setFixedSize(50,30); 

     signalMapper -> setMapping(button, texts[i]); 
     gridLayout -> addWidget(button, i/5, i%5); 
    } 

    calcButtons[0];//Use this to access them later. '0' being the first button added.