MT4 Manager API on QT
-
Hello Everyone,
I am new on QT and i was trying if i can use MT4 Manager API on QT since i want to improve the apps that i have created on MFC (c++). Was able to load the manager interface but i can't connect the manager to the server. Below are the code that i have on C++
.h
if (!AfxSocketInit()) { AfxMessageBox("Socket initialization failed."); return FALSE; } if (m_factory.IsValid() == FALSE || (m_manager = m_factory.Create(ManAPIVersion)) == NULL || (m_pump = m_factory.Create(ManAPIVersion)) == NULL) { AfxMessageBox("Failed to create manager interface."); return FALSE; }
.cpp
int res = -1; res = m_manager->Connect("access.metatrader5.com:443"); if (res != RET_OK) MessageBox("Server connection failed.");
And on QT which i have based the initialization of the socket using QTcpSocket.
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_connect); socket = new QTcpSocket(this); if(!socket) QMessageBox::critical(this, "Socket initialization Failed", "Failed to create socket"); } MainWindow::~MainWindow() { delete ui; if(m_manager) { m_manager->Release(); } } void MainWindow::on_connect() { int res =-1; QMessageBox msgbox; if(m_factory.IsValid()== FALSE || (m_manager = m_factory.Create(ManAPIVersion)) == NULL) { msgbox.setText("m_factory is invalid."); msgbox.exec(); } else { res = m_manager->Connect("access.metatrader5.com:443"); if(res != RET_OK) { msgbox.setText(m_manager->ErrorDescription(res)); msgbox.exec(); } } }
On MFC, i just initialize the socket (AfxSocketInit()) to connect using m_manager->connect("access.metaquotes.net:443") and was able to connect.
I hope you can help me on how to properly connect using socket on QT.
Thanks
-
@Blustack3r said in MT4 Manager API on QT:
I hope you can help me on how to properly connect using socket on QT.
Did you try QAbstractSocket::connectToHost()?
-
Hi,
The only thing is that, if you check the C++ code, you will just need to initialize the socket. The way i see it on QT, you will need to "connect" the socket to the IP address of which if you will check again on the C++ code, the "m_manager" already has the function "connect"
-
@Blustack3r said in MT4 Manager API on QT:
The only thing is that, if you check the C++ code, you will just need to initialize the socket.
Currently your
QTcpSocket
does nothing. You create it heresocket = new QTcpSocket(this);
and then never use it, AFAICS.
The way i see it on QT, you will need to "connect" the socket to the IP address of which if you will check again on the C++ code, the "m_manager" already has the function "connect"
I don't know what MT4 Manager does or how it works.
If you want to useQTcpSocket
you need to use theQTcpSocket
API to connect to some host.
(like @JonB said above)
However:
Since the MT4 API is probably incompatible withQTcpSocket
by default and you probably need to use the Manager'sconnect
function to make everything that is going on in the background work properly, I don't see why you need to use the Qt socket classes, assuming this MT4 Manager has its own internal socket stuff?!
I could be wrong, though.Just check/compare your code with this
(dont even know if this is the API you are using)Edit:
Only because you have Qt in your app, you are not forced to use the
Q
-classes everywhere.
You still have a "regular" C++ app and as long as you can't (or don't want to) wrap around this MT4 Manager, I think life is easier when doing it the recommended (MT4-) way. Just be careful not to block the Qt event loop and queue and you are good to go. -
Hi.
Thank you for the explanation.
@Pl45m4 said in MT4 Manager API on QT:
Since the MT4 API is probably incompatible with QTcpSocket by default and you probably need to use the Manager's connect function to make everything that is going on in the background work properly, I don't see why you need to use the Qt socket classes, assuming this MT4 Manager has its own internal socket stuff?!
I could be wrong, though.Yes this is correct. The only thing is that, when i use the MT4 Manager to connect to the server, it always returns "no connection" error, which means, the MT4 Manager can't establish connection to the server, of which the server is online and i have run the C++ program with the same server credentials.