如何将 QRadioButton 连接到 QTabWidget?

发布于 2024-09-16 20:52:47 字数 3649 浏览 1 评论 0原文

我正在使用 C++ 和 QT 创建一个软件,我有两个小部件,一个是 QRadioButon 类型,一个是 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);
}

i am creating a software using C++ and QT,and i have two widget one of type QRadioButon and one QTabWidget. My need is that i want to send signal from a radio button and i want that whenever the button is checked the content of tab get changed.
Can anyone suggest how to do that? I tried creating a slot of my widget class and in that slot i called the constructor of tab class but the problem is the construtor is not getting called.
here is the code i am using..

#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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

差↓一点笑了 2024-09-23 20:52:47

在 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();
}

并且您无法将信号连接到构造函数。

In the header definition file of your v_lab class you should have something like this:

// 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();
}

And you cannot connect a signal to a constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文