2016-06-12 45 views
-1

我做了一個應用程序來練習C++和Qt(初級)複選框切換QPushbutton功能(追加字符串+顯示自定義圖標)

如下:(!麻將主題) enter image description here

自動取款機,它只是這樣的: http://imgur.com/cnXfWZh

很簡單,但我想添加一些功能。

問題是,我不認爲添加它們會很困難(至少對我而言)。

「玩家1」 複選框

  • 只有滴答 「玩家1」 複選框,應在GIF看到的動作被允許。 (現在它總是被允許的;不需要勾選框)

  • 當勾選該框時,它也應該添加每個瓷磚圖標的「graphicsView」區域,因爲每個瓷磚點擊,在同一順序如「textEdit box 1」中的輸出中所見。

    (在「文字編輯框1」,這已經是目前工作的字符串附加功能的頂部)

  • 最多13張牌應該在「文字編輯」複選框和「graphicsView」區域

    被允許

像這樣:http://imgur.com/SUWHJSK

我節省了 「牆」 複選框應該爲以後做什麼:)

我的代碼可能是災難性的,對此感到遺憾。 < - 還是很初級,但這裏有雲:

.pro文件:

QT  += core gui 

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 

TARGET = HandBuilder_Mahjong 
TEMPLATE = app 


SOURCES += main.cpp\ 
     mainwindow.cpp 

HEADERS += mainwindow.h 

FORMS += mainwindow.ui 

RESOURCES += \ 
    tiles.qrc 

mainwindow.h:

private slots: 
    void addTextToLabel(); 
    void addTextToLabel2(); 
    void addTextToLabel3(); 
    void addTextToLabel4(); 
    void addTextToLabel5(); 
    void addTextToLabel6(); 
    void addTextToLabel7(); 
    void addTextToLabel8(); 
    void addTextToLabel9(); 
    void addTextToLabel10(); 
    void addTextToLabel11(); 
    void addTextToLabel12(); 
    void addTextToLabel13(); 
    void addTextToLabel14(); 
    void addTextToLabel15(); 
    void addTextToLabel16(); 
    void addTextToLabel17(); 
    void addTextToLabel18(); 
    void addTextToLabel19(); 
    void addTextToLabel20(); 
    void addTextToLabel21(); 
    void addTextToLabel22(); 
    void addTextToLabel23(); 
    void addTextToLabel24(); 
    void addTextToLabel25(); 
    void addTextToLabel26(); 
    void addTextToLabel27(); 

    void addTextToLabel28(); 
    void addTextToLabel29(); 
    void addTextToLabel30(); 
    void addTextToLabel31(); 
    void addTextToLabel32(); 
    void addTextToLabel33(); 
    void addTextToLabel34(); 





private: 
    Ui::MainWindow *ui; 
}; 

#endif // MAINWINDOW_H 

main.cpp中:

#include "mainwindow.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 

    return a.exec(); 
} 

mainwindow.cpp:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    connect(ui->Bamboo_1, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel())); 
    connect(ui->Bamboo_2, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel2())); 
    connect(ui->Bamboo_3, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel3())); 
    connect(ui->Bamboo_4, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel4())); 
    connect(ui->Bamboo_5, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel5())); 
    connect(ui->Bamboo_6, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel6())); 
    connect(ui->Bamboo_7, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel7())); 
    connect(ui->Bamboo_8, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel8())); 
    connect(ui->Bamboo_9, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel9())); 

    connect(ui->Man_1, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel10())); 
    connect(ui->Man_2, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel11())); 
    connect(ui->Man_3, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel12())); 
    connect(ui->Man_4, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel13())); 
    connect(ui->Man_5, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel14())); 
    connect(ui->Man_6, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel15())); 
    connect(ui->Man_7, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel16())); 
    connect(ui->Man_8, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel17())); 
    connect(ui->Man_9, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel18())); 

    connect(ui->Pin_1, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel19())); 
    connect(ui->Pin_2, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel20())); 
    connect(ui->Pin_3, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel21())); 
    connect(ui->Pin_4, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel22())); 
    connect(ui->Pin_5, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel23())); 
    connect(ui->Pin_6, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel24())); 
    connect(ui->Pin_7, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel25())); 
    connect(ui->Pin_8, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel26())); 
    connect(ui->Pin_9, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel27())); 

    connect(ui->Wind_East, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel28())); 
    connect(ui->Wind_North, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel29())); 
    connect(ui->Wind_South, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel30())); 
    connect(ui->Wind_West, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel31())); 
    connect(ui->Dragon_Red, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel32())); 
    connect(ui->Dragon_Green, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel33())); 
    connect(ui->Dragon_White, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel34())); 


} 



//Bamboo tiles 

void MainWindow::addTextToLabel() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b1"); 
} 

void MainWindow::addTextToLabel2() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b2"); 
} 

void MainWindow::addTextToLabel3() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b3"); 
} 

void MainWindow::addTextToLabel4() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b4"); 
} 

void MainWindow::addTextToLabel5() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b5"); 
} 

void MainWindow::addTextToLabel6() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b6"); 
} 

void MainWindow::addTextToLabel7() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b7"); 
} 

void MainWindow::addTextToLabel8() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b8"); 
} 

void MainWindow::addTextToLabel9() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";b9"); 
} 

//Man tiles 

void MainWindow::addTextToLabel10() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n1"); 
} 

void MainWindow::addTextToLabel11() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n2"); 
} 

void MainWindow::addTextToLabel12() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n3"); 
} 

void MainWindow::addTextToLabel13() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n4"); 
} 

void MainWindow::addTextToLabel14() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n5"); 
} 

void MainWindow::addTextToLabel15() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n6"); 
} 

void MainWindow::addTextToLabel16() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n7"); 
} 

void MainWindow::addTextToLabel17() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n8"); 
} 

void MainWindow::addTextToLabel18() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n9"); 
} 


// Pin tiles 
void MainWindow::addTextToLabel19() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p1"); 
} 

void MainWindow::addTextToLabel20() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p2"); 
} 

void MainWindow::addTextToLabel21() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p3"); 
} 

void MainWindow::addTextToLabel22() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p4"); 
} 

void MainWindow::addTextToLabel23() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p5"); 
} 

void MainWindow::addTextToLabel24() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p6"); 
} 

void MainWindow::addTextToLabel25() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p7"); 
} 

void MainWindow::addTextToLabel26() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p8"); 
} 

void MainWindow::addTextToLabel27() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";p9"); 
} 



//Wind tiles 
void MainWindow::addTextToLabel28() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";e"); 
} 

void MainWindow::addTextToLabel29() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";n"); 
} 

void MainWindow::addTextToLabel30() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";s"); 
} 

void MainWindow::addTextToLabel31() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";w"); 
} 




//Dragon tiles 
void MainWindow::addTextToLabel32() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";rd"); 
} 

void MainWindow::addTextToLabel33() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";gd"); 
} 

void MainWindow::addTextToLabel34() 
{ 
    ui->textEdit->setText(ui->textEdit->toPlainText() + ";wd"); 
} 



MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

我真的很喜歡和欣賞一些幫助,哪怕在我描述它並不適合所有的東西。

(希望我不要求太多)

回答

1

好吧,我不看你的複選框的任何信號/槽連接,因此只是做了默認情況下,被選中和未選中狀態。如果你想你的複選框鏈接到文本編輯,考慮

connect(ui->player1_checkbox, SIGNAL(stateChanged(int)), this, SLOT(toggleTextEdit())); 

而且toggleTextEdit()可能是

void MainWindow::toggleTextEdit() 
     { 
     if(ui->player1_checkbox->isChecked()) 
      ui->textEdit->setEnabled(true); 
     else 
      ui->textEdit->setEnabled(false); 
     } 

而對於graphicsview,您可能需要創建一個大小13的陣列和更新數組在每次更改鼠標下焦點的按鈕,以一種方式排列的內容更新類似

array[counter%13] = currentButtonNumber; 
counter = counter+1; 

然後,您可以用對應的陣列圖標更新graphicsview元素。變量counter只是用來跟蹤點擊的按鈕,並相應地更新數組,從每13次點擊0開始。 希望這個作品。

+0

這是幫助。我會試試看,然後看圖形視圖更新。 – Shamon

相關問題