问题描述
我想从我的mainwindow.cpp
访问对话型窗口小部件的某些功能.
I'd like to access some functions of a dialog-type widget from my mainwindow.cpp
.
我在小部件类中创建了一个简单的函数,该函数返回一个int,如下所示:
I've created a simple function in the widget class that returns an int, which looks like this:
dialog.h:
public:
int getRowCountData();
dialog.cpp:
dialog.cpp:
int Dialog::getRowCountData()
{
return ui->tableWidget->rowCount();
}
用法:
我的mainWindow.h:
my mainWindow.h:
private:
Dialog *dialog;
我的mainwindow.cpp:
my mainwindow.cpp:
dialog = new Dialog;
int count = dialog->getRowCountData(); <<<<<--------------this throws a read access error!
代码:0xc0000005读取访问冲突:0x0 flas = 0x0
我如何使用另一个小部件的公共功能,例如这个简单的功能?
how am I to use another widget's public functions such as this simple one?
我一定不能引用我要使用rowcount函数设置的整数.过去,信号和插槽在它们之间在小部件之间传输数据时对我来说非常有用,但我想尽可能使用这样的对话框小部件功能.
I must not be referencing the integer I'd like to set using the rowcount function. Signals and slots have worked great for me in the past when using them to transfer data between widgets, but I'd like to stick to using my dialog widget's function like this if I can.
通过发布这个问题:从另一个类访问QTableWidget的数据,我想到了这种在其他小部件上检索数据的方法. @Chernobyl也许您还有其他需要提供的见识?
I came about this method for retrieving data on my other widget by posting this question: AccessingQTableWidget's data from another class. @Chernobyl maybe you have some further insight to provide?
提前谢谢!
推荐答案
否.使用错误的指针时出现问题.这种执行就是这个意思.
No. Problem in usingwrong pointer. This execption means exactly this.
检查对话框指针是否不为空,并且ui-> tableWidget不存在.如果要使用在Qt Designer中创建的小部件,应调用setupUi
Check is dialog pointer isn't null and maybe ui->tableWidget doesn't exist. You should call setupUi if you want use widgets which created in Qt Designer
写这个:
if(!dialog)
qDebug() << "fail";
else
{qDebug() << "good";//your call}
大多数情况下,此类崩溃是由
Most times such a crash happens is caused by
- 多次删除对象
- 访问悬空指针(即指向已删除指针的指针)对象)
- 访问空指针
- deleting an object more than once
- accessing a dangling pointer (i.e. a pointer to an already deletedobject)
- accessing a null pointer
这篇关于(Qt)尝试使用MainWindow上其他窗口小部件类中的任何功能时出现读取访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!