我在用 Qt 编写的 GUI 上有一个按钮。这个按钮所做的是将一个整数值增加 2。然后这个新值显示在同一个 GUI 上的一个小部件中。我的目标是将这个增加的值传递给原始数据类型,在本例中为 int。
递增按钮允许您递增显示在 GUI 上的值,这意味着我在这两个 GUI 元素之间使用了 SIGNAL 和 SLOT 协议(protocol)。
SIGNAL 和 SLOT 不同于 Action 监听器协议(protocol),因此,我似乎无法更新存储此值的 int 每次鼠标按钮单击。每次用户单击 GUI 上的递增按钮时,我都需要能够更新此 int 值……最终目标是将此值传递给另一个用 C++ 编写的数学函数。下面是我试图弄清楚如何做到这一点的尝试。请注意,在整个 GUI 运行过程中,只会打印出一个值,即存储在 outVal
中的初始值。如果这使用了 Action 监听器,那么这将不是问题。所以我的问题是,每次单击鼠标时如何更新 outVal
...谢谢!
#include <QApplication>
#include <QGridLayout>
#include <QLabel>
#include <QSpinBox>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#define MIN_WIDTH 350
#define MIN_HEIGHT 300
int main(int argc, char ** argv){
int outVal = 99;
QApplication a(argc, argv);
QWidget window;
window.setMinimumSize(MIN_WIDTH,MIN_HEIGHT);
QVBoxLayout * mainPlatter = new QVBoxLayout(&window);
QSpinBox * spinBox = new QSpinBox;
spinBox->setRange(1,77);
spinBox->setSingleStep(2);
QLabel * label = new QLabel("1");
mainPlatter->addWidget(spinBox);;
mainPlatter->addWidget(label);
QObject :: connect( spinBox, SIGNAL(valueChanged(int)) , label, SLOT(setNum(int)));
outVal = spinBox->value();
window.show();
printf("%d\n",outVal);
return a.exec();
return 0;
}
编辑:在我得到建议后,这就是我尝试过的
#include <iostream>
#include <QObject>
using namespace std;
class Counter : public QObject{
Q_OBJECT
signals:
void valueChanged(int newValue);
public:
Counter(){
m_value = 1;
}
int value() const {
return m_value;
}
public slots:
void setValue(int value);
private:
int m_value;
};
void Counter::setValue(int value){
if(value != m_value){
m_value = value;
emit valueChanged(value);
}
}
int main(void){
Counter a, b;
QObject :: connect(&a, SIGNAL(valueChanged(int)), &b, SLOT(setValue(int)));
a.setValue(7);
cout << "a.setValue(7) " << endl;
cout << "(a.value, b.value) = " << a.value() << b.value() << endl;
return 0;
}
这些是我的错误
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/share/qt5/mkspecs/linux-g++-64 -I. -我。 -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o example1.o example1.cpp
g++ -m64 -Wl,-O1 -o gui1 example1.o -L/usr/X11R6/lib64 -lQt5Widgets -L/usr/lib/x86_64-linux-gnu -lQt5Gui -lQt5Core -lGL -lpthread
example1.o:在函数 Counter::setValue(int)' 中:
example1.cpp:(.text+0x9): undefined reference
Counter::valueChanged(int)'
example1.o:在函数 main' 中:
example1.cpp:(.text.startup+0x25): undefined reference
计数器的 vtable'
example1.cpp:(.text.startup+0x94): 未定义对 `Counter::valueChanged(int)' 的引用
collect2:错误:ld 返回 1 退出状态
make: * [gui1] 错误 1
仅供引用;我先运行“qmake -project”,然后运行“qmake”,然后运行“make”。
编辑:最终的工作版本!这个故事的寓意是,使用头文件、函数定义和驱动程序在单独的 c++ 文件中将所有内容放在适当的位置,这使使用 QT 时的生活变得更加轻松。这里的具体问题是 MOC 没有运行,因为驱动程序(主)和 QObject 类在同一个文件中。
//function.h
#ifndef FUNCTION_H
#define FUNCTION_H
#include <QObject>
class Counter : public QObject {
Q_OBJECT
public:
Counter(){
m_value = 1;
}
int value() const {
return m_value;
}
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
#endif
//function.cpp
#include "function.h"
void Counter :: setValue(int value){
if(value != m_value){
m_value = value;
emit valueChanged(value);
}
}
//main.cpp
#include <iostream>
#include <QObject>
#include "function.h"
using namespace std;
int main(void){
Counter a, b;
QObject :: connect(&a, SIGNAL(valueChanged(int)), &b, SLOT(setValue(int)));
a.setValue(7);
cout << "a.setValue(7) " << endl;
cout << "(a.value, b.value) = " << a.value() << b.value() << endl;
return 0;
}
最佳答案
The increment button allows you to increment a value which is displayed on the GUI, this means that ive (sic) used a SIGNAL and SLOT protocol between these two GUI elements
这就是问题所在。您应该做的是将任何数据模型与其可视化表示分开。如果将数据存储在一个类中,我们称它为模型,然后将按钮连接到模型类中的槽,槽函数可以更新其值,然后发出信号以更新数据 View 。
例如:
class Model : public QObject
{
Q_OBJECT
signals:
// connect this signal to the spin box setValue slot
void UpdateView(int value);
public slots:
// connect this slot to the push button released() signal
void IncrementValue();
private:
int m_value;
}
void Model::IncrementValue()
{
m_value += 2;
emit UpdateView(m_value);
}
有一个很棒的设计模式可以扩展这个叫做 MVC ( Model View Controller )
关于c++ - 将数据从 Qt 传递到 C++ 中的原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21191558/