Qt Creator-向梳形框添加大量值

Qt Creator-向梳形框添加大量值,qt,combobox,qt-creator,Qt,Combobox,Qt Creator,我有一个很长的数字491列表,我想把它们都放进Qt Creator中的combbox小部件中。手工输入是一种选择,但我觉得似乎有一种更有效的方法将数字添加到框中。 有什么建议吗?您可以在存储容器上迭代并添加它们 QComboBox *comboBox; comboBox = new QComboBox( this ); comboBox ->setGeometry( 100 , 100 , 100 , 20 ); QList< int > intList; intList

我有一个很长的数字491列表,我想把它们都放进Qt Creator中的combbox小部件中。手工输入是一种选择,但我觉得似乎有一种更有效的方法将数字添加到框中。
有什么建议吗?

您可以在存储容器上迭代并添加它们

QComboBox *comboBox;

comboBox = new QComboBox( this );
comboBox ->setGeometry( 100 , 100 , 100 , 20 );

QList< int > intList;

intList.append( 1 );
intList.append( 2 );
intList.append( 3 );
intList.append( 4 );
intList.append( 5 );
intList.append( 6 );
intList.append( 7 );

foreach( const int &i , intList )
{
    comboBox->addItem( "Item: " + QString::number( i ) , i );
}
还是我误解了你的问题

要将文本文件中的数据转换为QStringList,只需执行以下操作:

    QFile       file( "/path/filename.txt" );
    QStringList stringList;


    if( ( !file.fileName( ).trimmed( ).isEmpty( ) ) &&
        (  file.exists( ) ) && 
        (  file.open( QIODevice::ReadOnly | QIODevice::Text ) ) )
    {
        QTextStream textStream( &file );

        while( !textStream.atEnd( ) )
        {
            stringList << textStream.readLine( );
        }

        file.close( );

    // then just fill the QComboBox Text.
    comboBox->addItems( stringList );

号码存储在哪里?它们是在一个用逗号分隔的文本文件中还是在一个新行中?组合框中的500个条目听起来像是很棒的用户界面。Nejat->.txt文件有新行。Osterfeld->最佳。@Sluxxy添加了一个解决方案,其中包括将文本文件读入QStringList并将其设置为QComboBox内容。如果有任何不清楚的地方,请告诉我如何向现有组合框添加更多值?@Sluxxy的工作方式与此相同,只是您不必创建新的QComboBox对象。嘿,谢谢您的回答,但值的数量只是大小的八分之一。谢谢你的回答。我只是不需要它。@sluxxy即使是八分之一的尺寸,你也可以用它