[英]QTreeWidget::currentItem What returns when there is nothing selected?
I am a student programmer using Qt to develop a GUI application. 我是一名使用Qt开发GUI应用程序的学生程序员。 I am using a QTreeWidget to display some properties stored in a vector.
我正在使用QTreeWidget来显示存储在向量中的某些属性。 In the same window I have the buttons edit, copy, and delete.
在同一窗口中,我具有编辑,复制和删除按钮。 So far the buttons work to do what they need to;
到目前为止,按钮可以完成所需的操作; however I am having an issue when nothing is selected.
但是,当什么都没有选择时,我遇到了一个问题。 My program unexpectedly finishes;
我的程序意外完成; I am guessing a seg fault.
我猜是段错误。 I dont think I am handling the currentItem selected correctly.
我认为我没有正确选择currentItem。 However Qt's Documentation on this doesn't say what is returned if nothing is selected.
但是, Qt的文档并未说明如果未选择任何内容将返回什么。 So I was hoping for someone with more experience to help/enlighten me on this one.
因此,我希望有更多经验的人对此有所帮助/启发。 If there is something youd like to see out side of the code included just ask.
如果您想查看包含的代码之外的内容,请询问。 Here is my (relevant) code:
这是我的(相关)代码:
#include "injectiongui.h"
#include "ui_injectiongui.h"
#include "injectiondialog.h"
#include "ui_injectiondialog.h"
#include "injectiondata.h"
#include <QMessageBox>
InjectionGUI::InjectionGUI(QWidget *parent) :
QWidget(parent),
ui(new Ui::InjectionGUI)
{
ui->setupUi(this);
ui->groupBoxGlobalParticlesPerCell->hide();
connect(ui->pushButtonEdit, SIGNAL(clicked()), this, SLOT(slotInjectionEdit()));
}
void InjectionGUI::buildTreeWidget()
{
//Build or Refresh Tree Widget with info from the vector
}
void InjectionGUI::editInjection_Clicked(QTreeWidgetItem *itemToEdit) //Creates an Injection Dialog to edit an Item from the Vector
{
QString converter = itemToEdit->text(0);
int id = converter.toInt();
InjectionDialog editInjectionDialog;
InjectionData presetValues;
if(itemToEdit == 0) // this was my attempt to handle nothing selected
{
QMessageBox invalidSelection;
invalidSelection.setText("Error: No row selected to edit");
return;
}
presetValues = qTreeInjectionData.at(id);
editInjectionDialog.setData(presetValues);
presetValues = editInjectionDialog.getData();
editInjectionDialog.exec();
qTreeInjectionData.replace(id, editInjectionDialog.transInjectionData);
buildTreeWidget();
}
void InjectionGUI::slotInjectionEdit()
{
editInjection_Clicked(ui->treeWidgetInjections->currentItem());
}
I tried using qDebug to find out what is being returned but I think its having issues getting the value of the itemToEdit because its a QwidgetTreeItem. 我尝试使用qDebug来查找返回的内容,但我认为它在获取itemToEdit的值时遇到问题,因为它具有QwidgetTreeItem。 Please only leave productive feedback as I am only interested in learning and overcoming the challenge.
请只留下有效的反馈,因为我只对学习和克服挑战感兴趣。 Thanks in advance!
提前致谢!
If no item is selected, you should assume that the currentItem()
method returns a NULL pointer: 如果未选择任何项目,则应假定
currentItem()
方法返回NULL指针:
QString converter = itemToEdit->text(0);
Trying to call a method from a null pointer is undefined behaviour, and it will most likely cause a segmentation fault. 尝试从空指针调用方法是未定义的行为,并且很可能会导致分段错误。 So you should add something like:
因此,您应该添加以下内容:
if(itemToEdit == NULL) {
// error handling, most likely a simple return
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.