如何从Linux应用程序发送ifconfig
How to send ifconfig from a Linux application?
我在Linux上写一个程序。我想在选项卡上阅读ifconfig和屏幕。我不知道该怎么做。
我找到了这个例子。我想升级一下。
我有很多错误
main.cpp: In function ‘int main(int, char**)’:
main.cpp:7:19: error: variable ‘QApplication app’ has initializer but incomplete type
/usr/include/qt4/QtGui/qtabwidget.h:167:14: error: ‘QTabBar* QTabWidget::tabBar() const’ is protected
main.cpp:21:14: error: within this context
main.cpp:21:15: error: invalid use of incomplete type ‘struct QTabBar’
/usr/include/qt4/QtGui/qtabwidget.h:56:7: error: forward declaration of ‘struct QTabBar’
main.cpp:21:33: error: incomplete type ‘QTabBar’ used in nested name specifier
main.cpp:21:63: error: ‘button3’ was not declared in this scope
main.cpp:23:1: error: ‘myprocess’ was not declared in this scope
main.cpp:25:11: error: ‘ps’ was not declared in this scope
main.cpp:26:9: error: ‘myTabWidget’ was not declared in this scope
make: *** [main.o] Error 1
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow *window = new QMainWindow();
window->setWindowTitle(QString::fromUtf8("MainWindow"));
window->resize(480, 480);
QWidget *centralWidget = new QWidget(window);
QTabWidget *tabs = new QTabWidget(centralWidget);
tabs->setFixedSize(440, 440);
tabs->addTab(new QWidget(),"TAB 1");
tabs->addTab(new QWidget(),"TAB 2");
tabs->addTab(new QWidget(),"TAB 3");
tabs->tabBar()->setTabButton(2, QTabBar::LeftSide,((QWidget*)(button3)));
QProcess myProcess;
myprocess.start("ifconfig");
if (myProcess.waitForStarted(-1)) {
while(ps.waitForReadyRead(-1)) {
myTabWidget.setText(1, myprocess.readAllStandardOutput());
}
}
window->setCentralWidget(centralWidget);
window->setCentralWidget(centralWidget);
window->show();
return app.exec();
}
您正在寻找QProcess和/或QtNetwork以将此信息获取到您的选项卡小部件中。
...
tabs->tabBar()->setTabButton(2, QTabBar::LeftSide,((QWidget*)(button3)));
QProcess myProcess;
myprocess.start("ifconfig");
if (myProcess.waitForStarted(-1)) {
while(ps.waitForReadyRead(-1)) {
myTabWidget.setText(your_index, myprocess.readAllStandardOutput());
}
}
window->setCentralWidget(centralWidget);
...
话虽这么说,你可以看看QtNetwork更便携,因为"ifconfig"将无法在Windows上工作,等等。您需要将其更改为ipconfig,等等。
公平地说,即使在Linux上,您也应该查看"ip"命令,而不是旧的且难以维护的"ifconfig"命令。
因此,您将寻找具有适当跨平台解决方案的QNetworkInterface和QHostAddress。下面是一个简短的例子:
foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces())
qDebug() << interface.hardwareAddress();
foreach(const QHostAdress &address, QNetworkInterface::allAddresses())
qDebug() << address.toString();
如果您正在寻找的是IP地址,您可能希望使用gethostbyname()
而不是调用外部程序(如Linux上的ifconfig,或Windows上的ipconfig)。
如果您需要额外的信息(除了IP地址),您可能还需要考虑从/proc/net(仅限Linux)读取:
- http://www.onlamp.com/pub/a/linux/2000/11/16/LinuxAdmin.html
相关文章:
- 试图在visual studio上用C++创建一个桌面应用程序
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 在C应用程序中运行C++(带有STL)函数
- 使用VerQueryValue检索应用程序的文件描述
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用调试/崩溃报告将应用程序部署到客户端
- C++控制台应用程序阻止退出
- 码头化的C++应用程序是否向后兼容早期的内核版本
- 将应用程序从32位移植到64位时出现问题
- 如何改变c++应用程序的视觉效果
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- phytec phyBOARD iMX-6在从闪存而不是SD卡运行qt5 opengles应用程序时表现不佳(FPS减半
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 如何从Windows应用程序输出到标准?
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 如何从Linux应用程序发送ifconfig