Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCombobox
Qt 6.11 is out! See what's new in the release blog

QCombobox

Scheduled Pinned Locked Moved Unsolved General and Desktop
combobox
5 Posts 4 Posters 2.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I'm trying to add the new word created by the user in the combobox to a file using on_comboBox_editTextChanged.
    The code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->comboBox->setMaxVisibleItems (8);
        ui->comboBox->setEditable (true);
    
        QFile inputFile("C:/Programming/Projects/Qcombobox2/combo.txt");
    
        if(inputFile.open (QIODevice::ReadOnly | QIODevice::Text))
        {
    		QTextStream in(&inputFile);
            while(!in.atEnd ())
            {
                QString line = in.readLine ();
            	ui->comboBox->addItem (line);
            }
        }
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_comboBox_editTextChanged(const QString String)
    {
      String  == ui->comboBox->currentText ();
        QFile file("C:/Programming/Projects/Qcombobox2/combo.txt");
        if(file.open (QIODevice::WriteOnly | QIODevice::Text |QIODevice::Append))
           {
            QTextStream stream(&file);
    
            stream << String <<"\n";
    
            stream.flush ();
            file.close ();
    	}
    }
    
    

    The result in the text file:

    P
    Ph
    Pho
    Phon
    Phone
    What am I doing wrong? Should I be using a different signal? (The goal is to add the word to the file before the submit button is clicked).
    Thank you for your help.

    ValentinMicheletV jsulmJ 2 Replies Last reply
    0
    • G gabor53

      Hi,
      I'm trying to add the new word created by the user in the combobox to a file using on_comboBox_editTextChanged.
      The code:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          ui->comboBox->setMaxVisibleItems (8);
          ui->comboBox->setEditable (true);
      
          QFile inputFile("C:/Programming/Projects/Qcombobox2/combo.txt");
      
          if(inputFile.open (QIODevice::ReadOnly | QIODevice::Text))
          {
      		QTextStream in(&inputFile);
              while(!in.atEnd ())
              {
                  QString line = in.readLine ();
              	ui->comboBox->addItem (line);
              }
          }
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_comboBox_editTextChanged(const QString String)
      {
        String  == ui->comboBox->currentText ();
          QFile file("C:/Programming/Projects/Qcombobox2/combo.txt");
          if(file.open (QIODevice::WriteOnly | QIODevice::Text |QIODevice::Append))
             {
              QTextStream stream(&file);
      
              stream << String <<"\n";
      
              stream.flush ();
              file.close ();
      	}
      }
      
      

      The result in the text file:

      P
      Ph
      Pho
      Phon
      Phone
      What am I doing wrong? Should I be using a different signal? (The goal is to add the word to the file before the submit button is clicked).
      Thank you for your help.

      ValentinMicheletV Offline
      ValentinMicheletV Offline
      ValentinMichelet
      wrote on last edited by ValentinMichelet
      #2

      Hi,

      You have several signals from QLineEdit, especially editingFinished:
      http://doc.qt.io/qt-5/qlineedit.html#signals

      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Judging by the name of your function you connected it to the 'currentTextChanged' signal. It's emitted whenever current text changes, i.e. on every key stroke.
        If you are interested only in the final result you should override the focusOutEvent and keyReleaseEvent and watch for Enter/Return presses.
        Alternatively you can call lineEdit() on the combo to get the QLineEdit used and connect to its editingFinished() signal to get the same behavior.

        Btw. when passing strings as function params use const QString& value instead of const QString value. It saves you from making an unnecessary copy.

        1 Reply Last reply
        1
        • G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          Thanks. How can I watch for Enter/Return presses? The Go to slots option doesn't offer anything like that.

          1 Reply Last reply
          0
          • G gabor53

            Hi,
            I'm trying to add the new word created by the user in the combobox to a file using on_comboBox_editTextChanged.
            The code:

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                ui->comboBox->setMaxVisibleItems (8);
                ui->comboBox->setEditable (true);
            
                QFile inputFile("C:/Programming/Projects/Qcombobox2/combo.txt");
            
                if(inputFile.open (QIODevice::ReadOnly | QIODevice::Text))
                {
            		QTextStream in(&inputFile);
                    while(!in.atEnd ())
                    {
                        QString line = in.readLine ();
                    	ui->comboBox->addItem (line);
                    }
                }
            
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::on_comboBox_editTextChanged(const QString String)
            {
              String  == ui->comboBox->currentText ();
                QFile file("C:/Programming/Projects/Qcombobox2/combo.txt");
                if(file.open (QIODevice::WriteOnly | QIODevice::Text |QIODevice::Append))
                   {
                    QTextStream stream(&file);
            
                    stream << String <<"\n";
            
                    stream.flush ();
                    file.close ();
            	}
            }
            
            

            The result in the text file:

            P
            Ph
            Pho
            Phon
            Phone
            What am I doing wrong? Should I be using a different signal? (The goal is to add the word to the file before the submit button is clicked).
            Thank you for your help.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @gabor53 First get the QLineEdit instance of your QComboBox calling ui->comboBox->lineEdit().
            Then connect the editingFinished() signal of that QLineEdit with a slot. In the slot you can call ui->comboBox->lineEdit()->text() to get the string.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved