2010-08-31 45 views
0

我正在創建一個使用C++和QT的軟件,並且我有兩個類型的QRadioButon和一個QTabWidget。我的需要是我想從單選按鈕發送信號,我希望每當按鈕被選中時,標籤的內容就會被更改。 任何人都可以建議如何做到這一點?我試圖創建一個我的小部件類的插槽,並在那個槽中我稱爲tab類的構造函數,但問題是construtor沒有被調用。 這裏是我使用的代碼..如何將QRadioButton連接到QTabWidget?

#include <QtGui> 
#include "v_lab.h" 

v_lab::v_lab(QWidget *parent) 
    : QDialog(parent) 
{ 
    setWindowTitle("Virtual Lab"); 
    maingroup=new QGroupBox(this); 
    maingroup->setTitle("Algorithms"); 
    maingroup->setMinimumWidth(200); 
    maingroup->setMaximumWidth(240); 
    maingroup->setFlat(false); 
    p=new QPalette; 
    p->setColor(QPalette::Background,QColor(233,212,102)); 
    setPalette(*p); 

    box=new QGroupBox(maingroup); 
    box->setFlat(false); 
    box->setTitle("Searching Algorithm"); 

    linear_search=new QRadioButton("Linear Search",box); 
    linear_search->setChecked(1); 
    binary_search=new QRadioButton("Binary Search",box); 

    box1=new QGroupBox(maingroup); 
    box1->setFlat(false); 
    box1->setTitle("Sorting Algorithms"); 

    bubble_sort=new QRadioButton("Bubble Sort",box1); 
    selection_sort=new QRadioButton("Selection Sort",box1); 

    box2=new QGroupBox(maingroup); 
    box2->setFlat(false); 
    box2->setTitle("Tree Algorithms"); 

    infix_traversal=new QRadioButton("Infix Traversal",box2); 
    prefix_traversal=new QRadioButton("Prefix Traversal",box2); 
    postfix_traversal=new QRadioButton("Postfix Traversal",box2); 

    box3=new QGroupBox(maingroup); 
    box3->setFlat(false); 
    box3->setTitle("Graph Algorithms"); 

    bfs=new QRadioButton("BFS",box3); 

    dfs=new QRadioButton("DFS",box3); 
    shortest_path=new QRadioButton("Shortest Path",box3); 

    QString string1="go to hell"; 
    tab=new QTabWidget; 
    tab->addTab(new algorithm(string1),"Algorithm"); 
    // tab->addTab(new psudo_code(),"Pseduo-Code"); 
    tab->setMinimumWidth(250); 
    tab->setMaximumWidth(400); 

    //Layout 
    mainlayout=new QHBoxLayout(this); 
    mainlayout->addWidget(maingroup); 

    mainlayout->addWidget(tab); 
    mainlayout->addStretch(); 
    main_left_pane_layout=new QVBoxLayout(maingroup); 

    main_left_pane_layout->addWidget(box); 
    main_left_pane_layout->addWidget(box1); 
    main_left_pane_layout->addWidget(box2); 
    main_left_pane_layout->addWidget(box3); 

    left_pane_box=new QVBoxLayout(box); 

    left_pane_box->addWidget(linear_search); 
    left_pane_box->addWidget(binary_search); 

    left_pane_box1=new QVBoxLayout(box1); 

    left_pane_box1->addWidget(bubble_sort); 
    left_pane_box1->addWidget(selection_sort); 

    left_pane_box2=new QVBoxLayout(box2); 

    left_pane_box2->addWidget(infix_traversal); 
    left_pane_box2->addWidget(prefix_traversal); 
    left_pane_box2->addWidget(postfix_traversal); 

    left_pane_box3=new QVBoxLayout(box3); 

    left_pane_box3->addWidget(bfs); 
    left_pane_box3->addWidget(dfs); 
    left_pane_box3->addWidget(shortest_path); 

    connect(binary_search,SIGNAL(clicked()),this,SLOT(peeyush())); 
} 

algorithm::algorithm(const QString &string,QWidget *parent) 
    : QWidget(parent) 
{ 
    label=new QLabel(string); 
    main_layout=new QVBoxLayout; 
    main_layout->addWidget(label); 
    main_layout->addStretch(); 
    setLayout(main_layout); 
} 

/* 
psudo_code::psudo_code(QWidget *parent) 
    : QWidget(parent) 
{ 
    label1=new QLabel("Hello Peeyush Chandel"); 
    main_layout1=new QVBoxLayout; 
    main_layout1->addWidget(label1); 
    main_layout1->addStretch(); 
    setLayout(main_layout1); 
} 
*/ 

void v_lab::peeyush() 
{ 
    QString string1="new string"; 
    algorithm obj(string1); 
    //exit(1); 
} 
+0

請學習如何插入代碼。我的眼睛流血嘗試閱讀此。其次,你不能將信號連接到構造函數,這沒有任何意義。 – teukkam 2010-08-31 14:16:39

+0

我希望實際的代碼不會看起來這...偶然。另外,如何更改連接到單選按鈕的插槽中的選項卡*不工作? – 2010-08-31 14:23:25

回答

2

在你v_lab類的頭定義文件,你應該有這樣的事情:

// Includes here. 

class v_lab: public QDialog 
{ 
    Q_OBJECT // VERY important! 

public: 
    // Other things here. 

private slots: // VERY important. You can use public slots too. 
    void peeyush(); 
} 

而且你不能將信號連接到一個構造函數。