【发布时间】:2019-11-26 08:03:45
【问题描述】:
我制作了一个读取文件的程序。我有一个读取行号的组合框:1,6,11,..etc。我想例如当在组合框中选择第 1 行并单击按钮时读取第 1-5 行(或在选择第 6 行时读取第 6-10 行,以此类推)。 现在我有这个。
int line_counter=1;
if(file.open (QIODevice::ReadOnly | QIODevice::Text))
{
while(!stream.atEnd())
{
line = stream.readLine ();
if(!line.isNull ())
{
if((line_counter%5)==1)
ui->comboBox->addItem (line);
line_counter++;
}
}
}
stream.flush ();
file.close ();
void Servers::on_pushButton_clicked()
{
if(file.open (QIODevice::ReadOnly | QIODevice::Text))
{
for(int i=line_counter;i<line_counter+5;i++)
{
ui->textBrowser->setText(stream.readLine(i));
}
}
file.close ();
}
【问题讨论】:
-
代码有什么问题?你试过什么?
-
@SilvanoCerza 我无法将 ComboBox 与 TextBrowser 连接。当我选择 ComboBox 中的某些项目并单击 PushButton 时,我希望程序在 TextBrowser 中显示一些特定的行。我试过: void Servers::on_pushButton_clicked() { if(file.open (QIODevice::ReadOnly | QIODevice::Text)) { for(int i=line_counter;i
textBrowser- >setText(stream.readLine(i)); } } stream.flush ();文件.close(); } 但什么也没发生。 -
请编辑问题,无法在 cmets 中格式化代码。
-
@SilvanoCerza 好的,完成。抱歉,我是 StackOverFlow 的新手。
-
你的按钮在 ui 中是如何命名的?
标签: c++ qt combobox qcombobox qtextbrowser