Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8291591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:11:01+00:00 2026-06-08T13:11:01+00:00

I am using Qt to connect to databases (So far using PostgreSQL and mySQL)

  • 0

I am using Qt to connect to databases (So far using PostgreSQL and mySQL) and am a little confused on how the ports work. I am using Ubuntu if that makes a difference.

According to the documentation here: http://doc.qt.nokia.com/4.8-snapshot/qsqldatabase.html#setPort there is no default value for ports. However I noticed on my settings that I don’t actually set a port but it is still connecting without a problem.

So I have it set up like this:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("localhost");
db.setDatabaseName("mydb");
db.setUserName("name");
db.setPassword("pass");

Which works (I have tried with mySQL driver too)

It also works if i do this: db.setPort(-1); or as I would expect using the default port: db.setPort(5432);

But other ports don’t work which is as expected. So, given it says there is no default port value and -1 is obviously not a valid port how does it work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:11:02+00:00Added an answer on June 8, 2026 at 1:11 pm

    Qt itself may not provide a default port value, but the individual drivers do. When you open via Qt, it simply calls the open of the driver-specific code.

    For example, the Postgress driver code contains the following open function:

    bool QPSQLDriver::open(const QString & db, const QString & user,
      const QString & password, const QString & host,
      int port, const QString& connOpts)
    {
      if (isOpen()) close();
    
      QString conn;
    
      if (!host.isEmpty())
        conn.append(QLatin1String("host=")).append(qQuote(host));
      :
      if (port != -1)
        conn.append(QLatin1String(" port=")).append(qQuote(QString::number(port)));
      :
      d->connection = PQconnectdb(conn.toLocal8Bit().constData());
      :
      return true;
    }
    

    Similarly, the driver code for MySQL passes (port > -1) ? port : 0 as the port parameter to mysql_real_connect(), meaning that a value of -1 is translated to 0. And, as any MySQL coder will tell you, that informs mysql_real_connect() to use the default port.

    In other words, a port is translated from the QT-generic default of -1 to whatever the specific driver requires as a suitable default (such as 3306 for MySQL, 50000 (or 60000) for DB2, and so on).

    If you do set it, it’s passed to the driver as is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using pyodbc to connect to various odbc compliant databases. Suppose a MySql
I have multiple databases that I connect to using SQL*Plus in Windows (not the
I have a Pervasive database that I connect to using C++. All my queries
I'm using SQSH (version 2.1) on Ubuntu 10.04 to connect to a MSSQL database
I am a complete database newbie. So far, I know that I can connect
I Am Trying to create a program using c# that needs to connect to
trying to get my first little PHP script to connect to a local mySql
I'm confused when trying to fetch table rows in mysql using C++ with MySQL
I'm trying to connect to my mysql database but nothing seem to work out
I'm using PDO for connect to the database in a system where I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.