Qt中没有网络设置的组件,初步的想法是通过execvp函数调用shell命令进行网络设置。
所以首先,要保证shell命令的可用性,ifconfig,route等。
先创建一个文本框:
1 ipLabel = new QLabel(tr("&Ip:")); 2 ipLineEdit = new QLineEdit("192.168.1.224"); 3 ipLabel->setBuddy(ipLineEdit);
再设置输入格式和注册Field:
1 ipLineEdit->setInputMask("000.000.000.000;_"); 2 registerField("ip", ipLineEdit);
先提取文本框的字符串:
1 QString ipString = field("ip").toString(); 2 char ipBuf[20] = {0}; 3 strncpy(ipBuf, ipString.toStdString().c_str(), sizeof(ipBuf));
再通过宏进行设置IP:
1 Run("/sbin/ifconfig", "eth0", ipBuf, "netmask", maskBuf);
==============================================================================================
具体代码如下:
networkpage.h
1 #ifndef NETWORKPAGE_H 2 #define NETWORKPAGE_H 3 4 #include <QWizard> 5 6 QT_BEGIN_NAMESPACE 7 class QLabel; 8 class QLineEdit; 9 class QGridLayout; 10 class QRadioButton; 11 QT_END_NAMESPACE 12 13 class NetworkPage : public QWizardPage 14 { 15 Q_OBJECT 16 17 public: 18 NetworkPage(QWidget *parent = 0); 19 ~NetworkPage(); 20 bool validatePage (); 21 private: 22 int execute(char *arglist[]); 23 24 QLabel *ipLabel; 25 QLabel *maskLabel; 26 QLabel *gatewayLabel; 27 QLabel *dnsLabel; 28 QLineEdit *ipLineEdit; 29 QLineEdit *maskLineEdit; 30 QLineEdit *gatewayLineEdit; 31 QLineEdit *dnsLineEdit; 32 QGridLayout *layout; 33 QRadioButton *dhcpRadioButton; 34 QRadioButton *settingRadioButton; 35 }; 36 37 #endif /* NETWORKPAGE_H */
networkpage.cpp:
1 #include <QtGui> 2 #include <stdio.h> 3 #include <unistd.h> 4 #include <sys/types.h> 5 #include <sys/wait.h> 6 7 #include "networkpage.h" 8 9 #define Run(al0, al1, al2, al3, al4) {char *arglist[6]; \ 10 arglist[0]=al0; \ 11 arglist[1]=al1; \ 12 arglist[2]=al2; \ 13 arglist[3]=al3; \ 14 arglist[4]=al4; \ 15 arglist[5]=NULL; \ 16 if(execute(arglist)) \ 17 return 1;} 18 19 NetworkPage::NetworkPage(QWidget *parent) 20 : QWizardPage(parent) 21 { 22 setTitle(tr("Network setting")); 23 setSubTitle(tr("Specify where you want the wizard to put the generated " 24 "skeleton code.")); 25 setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo2.png")); 26 27 ipLabel = new QLabel(tr("&Ip:")); 28 ipLineEdit = new QLineEdit("192.168.1.224"); 29 ipLabel->setBuddy(ipLineEdit); 30 31 maskLabel = new QLabel(tr("&Mask:")); 32 maskLineEdit = new QLineEdit("255.255.255.0"); 33 maskLabel->setBuddy(maskLineEdit); 34 35 gatewayLabel = new QLabel(tr("&Gateway:")); 36 gatewayLineEdit = new QLineEdit("192.168.1.1"); 37 gatewayLabel->setBuddy(gatewayLineEdit); 38 39 dnsLabel = new QLabel(tr("&DNS:")); 40 dnsLineEdit = new QLineEdit("192.168.1.7"); 41 dnsLabel->setBuddy(dnsLineEdit); 42 43 ipLabel->setEnabled(false); 44 ipLineEdit->setEnabled(false); 45 maskLabel->setEnabled(false); 46 maskLineEdit->setEnabled(false); 47 gatewayLabel->setEnabled(false); 48 gatewayLineEdit->setEnabled(false); 49 dnsLabel->setEnabled(false); 50 dnsLineEdit->setEnabled(false); 51 52 ipLineEdit->setInputMask("000.000.000.000;_"); 53 maskLineEdit->setInputMask("000.000.000.000;_"); 54 gatewayLineEdit->setInputMask("000.000.000.000;_"); 55 dnsLineEdit->setInputMask("000.000.000.000;_"); 56 57 58 dhcpRadioButton = new QRadioButton(tr("DHCP")); 59 dhcpRadioButton->setChecked(true); 60 61 settingRadioButton = new QRadioButton(tr("setting")); 62 //settingRadioButton->setChecked(true); 63 64 registerField("ip", ipLineEdit); 65 registerField("mask", maskLineEdit); 66 registerField("gateway", gatewayLineEdit); 67 registerField("dns", gatewayLineEdit); 68 registerField("dhcp", dhcpRadioButton); 69 registerField("setting", settingRadioButton); 70 71 connect(settingRadioButton, SIGNAL(toggled(bool)), ipLabel, SLOT(setEnabled(bool))); 72 connect(settingRadioButton, SIGNAL(toggled(bool)), ipLineEdit, SLOT(setEnabled(bool))); 73 connect(settingRadioButton, SIGNAL(toggled(bool)), maskLabel, SLOT(setEnabled(bool))); 74 connect(settingRadioButton, SIGNAL(toggled(bool)), maskLineEdit, SLOT(setEnabled(bool))); 75 connect(settingRadioButton, SIGNAL(toggled(bool)), gatewayLabel, SLOT(setEnabled(bool))); 76 connect(settingRadioButton, SIGNAL(toggled(bool)), gatewayLineEdit, SLOT(setEnabled(bool))); 77 connect(settingRadioButton, SIGNAL(toggled(bool)), dnsLabel, SLOT(setEnabled(bool))); 78 connect(settingRadioButton, SIGNAL(toggled(bool)), dnsLineEdit, SLOT(setEnabled(bool))); 79 80 81 layout = new QGridLayout; 82 layout->addWidget(dhcpRadioButton, 0, 0, 1, 3); 83 layout->addWidget(settingRadioButton, 1, 0, 1, 3); 84 layout->addWidget(ipLabel, 2, 0); 85 layout->addWidget(ipLineEdit, 2, 1); 86 layout->addWidget(maskLabel, 3, 0); 87 layout->addWidget(maskLineEdit, 3, 1); 88 layout->addWidget(gatewayLabel, 4, 0); 89 layout->addWidget(gatewayLineEdit, 4, 1); 90 layout->addWidget(dnsLabel, 5, 0); 91 layout->addWidget(dnsLineEdit, 5, 1); 92 93 setLayout(layout); 94 95 } 96 97 NetworkPage::~NetworkPage() 98 { 99 delete ipLabel; 100 delete ipLineEdit; 101 delete maskLabel; 102 delete maskLineEdit; 103 delete gatewayLabel; 104 delete gatewayLineEdit; 105 delete dnsLabel; 106 delete dnsLineEdit; 107 delete dhcpRadioButton; 108 delete settingRadioButton; 109 delete layout; 110 } 111 112 int NetworkPage::execute(char *arglist[]) 113 { 114 int pid, exitstatus; 115 pid = fork(); 116 117 switch (pid) { 118 case -1: 119 perror("fork failed"); 120 return 1; 121 case 0: 122 execvp(arglist[0], arglist); 123 perror("execvp failed"); 124 return 1; 125 default: 126 while(wait(&exitstatus) != pid); 127 } 128 return 0; 129 } 130 131 bool NetworkPage::validatePage() 132 { 133 if (field("dhcp").toBool()) 134 { 135 printf("use dhcp\n"); 136 } 137 else if (field("setting").toBool()) 138 { 139 printf("use setting\n"); 140 141 QString ipString = field("ip").toString(); 142 QString maskString = field("mask").toString(); 143 QString gatewayString = field("gateway").toString(); 144 QString dnsString = field("dns").toString(); 145 146 char ipBuf[20] = {0}; 147 char maskBuf[20] = {0}; 148 char gatewayBuf[20] = {0}; 149 char dnsBuf[20] = {0}; 150 151 strncpy(ipBuf, ipString.toStdString().c_str(), sizeof(ipBuf)); 152 strncpy(maskBuf, maskString.toStdString().c_str(), sizeof(maskBuf)); 153 strncpy(gatewayBuf, gatewayString.toStdString().c_str(), sizeof(gatewayBuf)); 154 strncpy(dnsBuf, dnsString.toStdString().c_str(), sizeof(dnsBuf)); 155 156 Run("/sbin/ifconfig", "eth0", ipBuf, "netmask", maskBuf); 157 Run("/sbin/route add default gw", gatewayBuf, NULL, NULL, NULL); 158 Run("/bin/echo", "nameserver", dnsBuf, ">> /etc/resolv.conf", NULL); 159 } 160 161 //wizard()->next(); 162 return true; 163 }