gpt4 book ai didi

c++ - 如何使用 PushButton-Qt 连接 ComboBox 和 TextBrowser?

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:22 27 4
gpt4 key购买 nike

我制作了一个读取文件的程序。我有一个读取行号的组合框:1,6,11,..等。我想例如当在组合框中选择第 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 ();

}

最佳答案

如果文本已经在 comboBox 中,您可以避免从每个要更新的团队 textBrowser 中读取文件。

首先将按钮信号与您的方法连接:

connect(ui->pushButton, &QPushButton::clicked, this, &Servers::on_pushButton_clicked);

然后像这样改变on_pushButton_clicked:

void Servers::on_pushButton_clicked()
{
if(file.open (QIODevice::ReadOnly | QIODevice::Text))
{
int index = ui->comboBox->currentIndex();
int from = 5 * index;
int to = from + 5;
QTextStream stream(&file);
int lineCount = 0;
QString text;
QString line;
while (stream.readLineInto(&line)) {
if (from >= lineCount && lineCount < to) {
text += line;
text += '\n';
}
lineCount++;
}
ui->textBrowser->setText(text.toString());
}
}

关于c++ - 如何使用 PushButton-Qt 连接 ComboBox 和 TextBrowser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57076670/

27 4 0
文章推荐: html - 响应页面空白
文章推荐: c++ - 如何让定时器在后台保持运行
文章推荐: android - 如何在android模拟器中将可读权限更改为可写权限
文章推荐: html - 额外的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com