QT c++内部变量切换
QT c++ internal variable handover
我在mainwindow.h 中声明了一个int函数
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int port();
此外,我在appendant mainwindow.cpp文件中声明了这个函数。
int MainWindow::port()
{
int port_int;
QString port_ei;
if(ui->portInput->text() == 0)
{
port_ei = "6008";
ui->textIncome->setPlainText(port_ei);
port_int = port_ei.toInt();
}
else
{
port_ei = ui->portInput->text();
ui->textIncome->setPlainText(port_ei);
port_int = port_ei.toInt();
}
return port_int;
}
现在我想让我的服务器(在myserver.cpp文件中)监听那个端口。
MyServer::MyServer(QObject *parent) :
QObject(parent)
{
server = new QTcpServer(this);
connect(server,SIGNAL(newConnection()),this,SLOT(newConnection()));
int port_out = MainWindow::port();
if(!server->listen(QHostAddress::Any,port_out))
{
qDebug() << "Server could not start.";
}
else
{
qDebug() << "Server started.";
}
}
但qt告诉我,我对一个非静态函数(在int port_out = MainWindow::port();
行)进行了非法请求。如何解决?如果有两个单独的.cpp和.h文件(除了主.cpp之外),有没有更好的方法可以做到这一点。是的,我在两个cpp文件中都包含了"mainwindow.h"answers"myserver.h"。
MainWindow::port();
是一个静态函数调用。但是port
函数不是静态的,它需要像main_window->port()
一样被调用,其中main_window
是指向MainWindow
对象的指针。
相关文章:
- 内联函数中具有内部链接的全局变量
- C++ SSE 内部函数:将结果存储在变量中
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 是否可以禁止在for循环体内部修改循环变量
- 当使用lambda进行变量的复杂初始化时,如何处理从内部抛出的lambda外部异常
- 在变量名后声明带有 () 的非内部类型与不使用变量名的行为不同。即 std::map<int,char>x(); - 这是怎么回事?
- 使用虚拟变量对 std::vector 内部循环进行切片的最佳方法
- 访问lambda内部循环控制变量
- 如何通过从变量中获取类型来访问内部 typedef
- 解决具有嵌套模板化变量的 Visual Studio 内部编译器错误
- 内部循环的变量
- lambda内部捕获的constexpr变量失去了其constexpr-ness
- 循环时内部的C 声明和初始化变量
- 用名称为“ this”内部括号内的变量在3个不同的编译器上导致不同的结果
- 使用动态铸件来返回派生类内部的变量的不同值
- 如何在 for 循环内部或外部打印双精度变量
- 当在函数内部使用静态变量时,输出会发生变化,该函数在c++中返回引用
- 函数内部静态变量的内存分配
- 在外部功能中具有一个变量初始化或内部功能中的多个初始化是最有效的
- 将更改此静态变量内部的函数的结果分配给静态变量