Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将参数传递到qt5 c++; 我在C++ QT中创建了一个待办事项列表应用程序。当按下加号按钮时,它会将一个包含QLabel和QToolButton的QHBoxLayout添加到gui中的垂直布局中,因此我会得到一个包含“TODO”的新框,以及在框旁边删除它们的按钮。我在我的插槽中设置了各种小部件,单击add按钮时会调用这些小部件。但是,我需要将它们作为参数传递给在按下移除按钮时调用的插槽。我已经研究过了,我所发现的就是QSignalMapper。然而,我找不到任何与我的案例足够接近的案例来进行复制,我读过它只适用于某些参数,而不是我需要的三个参数(QHBoxLayout、QLineEdit和QToolButton)。 按下“添加”按钮时调用的插槽的部分代码为: //Creates a read only LineEdit which the user will add QLineEdit *toDoBox = new QLineEdit(this); toDoBox->setText(ui->lineEdit->text()); toDoBox->setReadOnly(true); //Creates a new X button for removal of ToDo's QToolButton *removeButton = new QToolButton; removeButton->setText("X"); //Adds a horizontal layout with the ToDo and the remove button in it, to keep them together QHBoxLayout *toDoLayout = new QHBoxLayout; toDoLayout->addWidget(toDoBox); toDoLayout->addWidget(removeButton); //Removes a ToDo when the remove button is clicked connect(removeButton, SIGNAL(clicked()), this, SLOT(on_removeButton_clicked()));_C++_Ios_Macos_Qt - Fatal编程技术网

将参数传递到qt5 c++; 我在C++ QT中创建了一个待办事项列表应用程序。当按下加号按钮时,它会将一个包含QLabel和QToolButton的QHBoxLayout添加到gui中的垂直布局中,因此我会得到一个包含“TODO”的新框,以及在框旁边删除它们的按钮。我在我的插槽中设置了各种小部件,单击add按钮时会调用这些小部件。但是,我需要将它们作为参数传递给在按下移除按钮时调用的插槽。我已经研究过了,我所发现的就是QSignalMapper。然而,我找不到任何与我的案例足够接近的案例来进行复制,我读过它只适用于某些参数,而不是我需要的三个参数(QHBoxLayout、QLineEdit和QToolButton)。 按下“添加”按钮时调用的插槽的部分代码为: //Creates a read only LineEdit which the user will add QLineEdit *toDoBox = new QLineEdit(this); toDoBox->setText(ui->lineEdit->text()); toDoBox->setReadOnly(true); //Creates a new X button for removal of ToDo's QToolButton *removeButton = new QToolButton; removeButton->setText("X"); //Adds a horizontal layout with the ToDo and the remove button in it, to keep them together QHBoxLayout *toDoLayout = new QHBoxLayout; toDoLayout->addWidget(toDoBox); toDoLayout->addWidget(removeButton); //Removes a ToDo when the remove button is clicked connect(removeButton, SIGNAL(clicked()), this, SLOT(on_removeButton_clicked()));

将参数传递到qt5 c++; 我在C++ QT中创建了一个待办事项列表应用程序。当按下加号按钮时,它会将一个包含QLabel和QToolButton的QHBoxLayout添加到gui中的垂直布局中,因此我会得到一个包含“TODO”的新框,以及在框旁边删除它们的按钮。我在我的插槽中设置了各种小部件,单击add按钮时会调用这些小部件。但是,我需要将它们作为参数传递给在按下移除按钮时调用的插槽。我已经研究过了,我所发现的就是QSignalMapper。然而,我找不到任何与我的案例足够接近的案例来进行复制,我读过它只适用于某些参数,而不是我需要的三个参数(QHBoxLayout、QLineEdit和QToolButton)。 按下“添加”按钮时调用的插槽的部分代码为: //Creates a read only LineEdit which the user will add QLineEdit *toDoBox = new QLineEdit(this); toDoBox->setText(ui->lineEdit->text()); toDoBox->setReadOnly(true); //Creates a new X button for removal of ToDo's QToolButton *removeButton = new QToolButton; removeButton->setText("X"); //Adds a horizontal layout with the ToDo and the remove button in it, to keep them together QHBoxLayout *toDoLayout = new QHBoxLayout; toDoLayout->addWidget(toDoBox); toDoLayout->addWidget(removeButton); //Removes a ToDo when the remove button is clicked connect(removeButton, SIGNAL(clicked()), this, SLOT(on_removeButton_clicked()));,c++,ios,macos,qt,C++,Ios,Macos,Qt,如果您想查看整个项目,我的代码位于GitHub上: 谢谢你对我的宽容——我很难解释清楚的事情 如果我很了解您的问题,您希望获得表示todo的已分配对象,以便释放它们并更新视图 您可以通过简单地将QLineEdit、QToolButton和QHBoxLayout对象包装到一个类中,并在ToDoList类中使用容器(例如向量)来实现这一点。这样,每次按下on_toolButton_clicked方法时,您都会将“todo对象”向后推 然后,您只需使用一个带有索引的信号来触发on_delete_to

如果您想查看整个项目,我的代码位于GitHub上:


谢谢你对我的宽容——我很难解释清楚的事情

如果我很了解您的问题,您希望获得表示todo的已分配对象,以便释放它们并更新视图

您可以通过简单地将QLineEdit、QToolButton和QHBoxLayout对象包装到一个类中,并在ToDoList类中使用容器(例如向量)来实现这一点。这样,每次按下on_toolButton_clicked方法时,您都会将“todo对象”向后推

然后,您只需使用一个带有索引的信号来触发on_delete_todo槽,该槽从向量中删除一个“todo对象”,并更新视图

还有,看看这个

这是一个样本(在QT5下测试和工作):

您的待办事项小部件
也可以使用的向量(一对int提供)。

我可以想出两种方法

  • 使用
  • 使用
    QSignalMapper

  • QObject::sender()
    此静态方法返回发出信号的发送方对象的
    QObject*
    。在本例中,
    QToolButton
    ,因此您可以使用此引用查找其父级(即
    QHBoxLayout
    )及其同级(即
    QLineEdit


    QSingalMapper

    首先定义一个列表以保存对所有行的引用,然后为每行分配一个唯一标识符(例如,行号),然后根据将该标识符用作每个按钮的
    QSignalMapper
    键。现在,如果用户单击插槽中的按钮,您将获得相同的标识符,使用它并在行列表中查找整行。

    信号和插槽或在文档中解释。更多细节在QtWiki中:(你应该使用新的语法,除非你有理由不这样做)。这是可行的,但是它经常崩溃(通常是在删除项目时,但是我没有充分测试它)。我做错什么了吗?我最近在github上提交的代码是新的。但是我还没有使用固定大小的向量,所以这可能是导致它的原因吗?当你添加一个TodoView对象时,你是在“推回”一个项目。这意味着向量总是在增长。但是,删除一个或多个项目时,max成员将递减。调用delete时,此操作在已删除的对象(=>segfault)上实现。删除后需要调整向量的大小。此外,max成员应初始化为-1,并在从向量中删除对象时测试发射索引的有效性。基本上,对象的索引和向量的索引不对应。这会在删除时导致segfault。每次调整向量大小时,都必须更新TodoView对象的索引。索引与向量的索引相同。我不确定我能想到的是:
    TodoVeiw*thisObject=TodoVeiw[I];thisObject->setIndex(i);删除第(i)段中的todoVeiw--最大值
    如何使用
    QObject::sender()获取
    QHBoxLayout
    QLineEdi
    t
    removeButton->parent()
    无法工作,因为它会获取中心小部件。我可以使用它来获取子项-
    QHBoxLayout
    QLineEdit
    ,但是当我删除它时,它会删除第一个集,而不是附加到removeButton的集
    #ifndef TODOVIEW_H
    #define TODOVIEW_H
    
    #include <QString>
    
    class QLineEdit;
    class QToolButton;
    class QHBoxLayout;
    
    #include <QWidget>
    
    class TodoView : public QWidget
    {
        Q_OBJECT
    
    private:
        QLineEdit*      frame;
        QToolButton*    removeButton;
        QHBoxLayout*    toDoLayout;
        int             index;
    
    public:
        TodoView(const QString& what, int index, QWidget* parent);
        ~TodoView();
    
        inline void setIndex(int i) { index = i; }
        inline int getIndex(){ return index; }
    
    private slots:
        void emitIndex();
    
    signals:
        void selectedIndex(int);
    
    };
    
    #endif // TODOVIEW_H
    
    #include "todoview.h"
    
    #include <QLineEdit>
    #include <QToolButton>
    #include <QHBoxLayout>
    
    TodoView::TodoView(const QString& what, int index, QWidget* parent) : QWidget(parent), index(index)
    {
        frame = new QLineEdit(this);
        frame->setText(what);
        frame->setReadOnly(true);
    
        removeButton = new QToolButton(this);
        removeButton->setText("X");
    
        toDoLayout = new QHBoxLayout(this);
        toDoLayout->addWidget(frame);
        toDoLayout->addWidget(removeButton);
    
        connect(removeButton, SIGNAL(clicked()), this, SLOT(emitIndex()));
    }
    
    TodoView::~TodoView() {}
    
    void TodoView::emitIndex()
    {
        emit selectedIndex(getIndex());
    }
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    #include <vector>
    
    class TodoView;
    class QVBoxLayout;
    
    namespace Ui {
        class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void addTodo();
        void delTodo(int);
    
    private:
        Ui::MainWindow*         ui;
    
        QVBoxLayout*            vBoxLayout;
    
        std::vector<TodoView*>  todoView;
        int                     max = -1;
    };
    
    #endif // MAINWINDOW_H
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include "todoview.h"
    
    #include <QVBoxLayout>
    #include <QAction>
    
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        vBoxLayout = new QVBoxLayout(centralWidget());
    
        QAction* add = new QAction(ui->mainToolBar);
        ui->mainToolBar->addAction(add);
    
        connect(add, SIGNAL(triggered()), this, SLOT(addTodo()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::addTodo()
    {
        if(max > 9)
        {
            // Error msg.
        }
        else
        {
            TodoView* tdV = new TodoView("Yolo", max, centralWidget());
            connect(tdV, SIGNAL(selectedIndex(int)), this, SLOT(delTodo(int)));
    
            vBoxLayout->addWidget(tdV);
            todoView.push_back(tdV);
            ++max;
        }
    }
    
    
    void MainWindow::delTodo(int i)
    {
        // check if i < todoView.size().
        delete todoView.at(i);
        // update vector indexes !!!
        --max;
    }
    
    todoView.reserve(10);
    for(std::size_t i = 0; i < 10; ++i)
    {
        todoView[i] = nullptr;
    }
    
    // Do not use push back.
    // retrieve the max index.
    // if < 10
    for(std::size_t i = 0; i < todoView.size(); ++i)
    {
        if(todoView[i] == nullptr)
        {
            // allocate TodoView and affect it to the i° element
        }
    }
    
    delete todoView[i];
    todoView[i] = nullptr;