combox如何獲取數據:
1.currentIndex(); 獲取當前comBox的索引,是int類型的值。
2.currentText(); 獲取當前comBox的文本,是QString類型。
3.currentData(int role = Qt::UserRole)獲取當前comBox綁定的數據,是QVariant類型。
具體使用方法:
QWidget * widget=ui->tableWidget->cellWidget(j,0);//獲得widget
QComboBox *combox=(QComboBox*)widget;//強制轉化爲QComboBox
double kernelkind=combox->currentIndex();//獲得索引數據
int QString str=combox->currentText();//獲取文本文檔
自己編寫的代碼:用來獲取combox和tablewidget中的內容
int *Dialog1::on_pushButton_2_clicked()
{
int num;
int j=5;
double *a=new double[j];
for(int j=0;j<2;j++)
{QWidget * widget=ui->tableWidget->cellWidget(j,0);//獲得widget
QComboBox *combox=(QComboBox*)widget;//強制轉化爲QComboBox
QString string=combox->currentText();//獲得combox中的文本內容
if(string==tr(""))//判斷內容是否爲空
{
{ QMessageBox::warning(NULL, "warning", "Content",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
break;
}
}
}
for(int j=2;j<5;j++)
{
if(ui->tableWidget->item(j,0)==NULL|| //判斷指向該cell的指針是否爲空
(ui->tableWidget->item(j,0)&&ui->tableWidget->item(j,0)->text()==tr("")))
{ QMessageBox::warning(NULL, "warning", "Content",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
break;
}
}
for(int j=0;j<2;j++)
{
QWidget * widget=ui->tableWidget->cellWidget(j,0);//獲得widget
QComboBox *combox=(QComboBox*)widget;//強制轉化爲QComboBox
double kernelkind=combox->currentIndex();
a[j]=kernelkind;
qDebug()<< a[j];
}
for(int j=2;j<5;j++)
{
QString s=ui->tableWidget->item(j,0)->text();//取出字符串
double num = s.toDouble();//轉成int
a[j]=num;
qDebug()<< a[j];
//a[j]=s.toInt();
}
num++;
//qDebug()<<j;
//delete[]a;
qDebug()<< a[4];
int t=one_against_one(a);
return a;
}