EzDevInfo.com

qt4 interview questions

Top qt4 frequently asked interview questions

How to run a system command in Qt?

I have to run a system command in Qt. but I have to give an argument for that command.

for example opening gedit with a text file. like "gedit /home/oDx/Documents/a.txt"

but the path "/home/oDx/Documents/a.txt" will be in a variable like "docPath". so how can i do it!?


Source: (StackOverflow)

How to "Reveal in Finder" or "Show in Explorer" with Qt

Is it possible to open a folder in Windows Explorer/OS X Finder and then select/highlight one file in that folder, and do it in a cross platform way? Right now, I do something like

QDesktopServices::openUrl( QUrl::fromLocalFile( path ) );

where path is a full path to folder I want to open. Obviously, this will just open the folder, and I'll have to track down the file I need manually. This is a bit of a problem when there are thousands of files in that folder.

If I make it a path to specific file in that folder, then that file is open with default application for that mime type, and that is not what I need. Instead, I need the functionality equivalent to "Reveal in Finder" or "Show in Explorer".


Source: (StackOverflow)

Advertisements

How to integrate Boost.Asio main loop in GUI framework like Qt4 or GTK

Is there any way to integrate Boost.Asio with Qt4 (preferred) or GTK main loop? GTK provides poll(2) like API so technically is should be possible. Qt provides its own networking layer, however I prefer to use existing code written for Boost.Asio. I want to integrate them without using an additional thread.

Is there any reference how to do this for Qt4 (preferred) or GTKmm?

Thanks.

Edit

I want to clearify several things to make the answer easier. Both Qt and GTKmm provide "select like" functionality:

So, the question is, how to integrate existing "selectors/pollers" as reactor to Boost.Asio io_service. Today, Boost.Asio can use select, kqueue, epoll, /dev/poll and iocp as reactor/proactor service. I want to integrate it to the main-loop of GUI framework.

Any suggestions and solutions (better) are welcome.


Source: (StackOverflow)

Is there a good source for 3rd party QT GUI controls? [closed]

I have tried to find them through Google but haven't had much luck. I know the number of 3rd party controls is not as big as .NET or Java, but surely there are some out there better than what comes with the library?


Source: (StackOverflow)

Get filename from QFile?

eg:

QFile f("/home/umanga/Desktop/image.jpg");

How I get only the filename - "image.jpg"?


Source: (StackOverflow)

Qt 4.5 - Is emitting signal a function call, or a thread, and does it blocks?

I am not sure about the nature of the signal/slot mechanism in Qt 4.5. When a signal is emitted, is it a blocking function call or a thread? Say this

emit GrabLatestData();

// proceed with latest data

Will all the signal/slot chain be resolved before proceeding to the next line?


Source: (StackOverflow)

Are signals in Qt automatically disconnected when one of the class is deleted

Does Qt automatically remove connections between objects , when one of the side is deleted ?

e.g connect (A .. , B ..) , when A (a pointer) is deleted , or B is deleted , will the connection be disconnected ?

Is it necessary to use disconnect explicitly in destructor ?


Source: (StackOverflow)

How to determine the platform Qt is running on at runtime?

Is there a (Qt) way to determine the platform a Qt application is running on at runtime?


Source: (StackOverflow)

What is the correct way of QSqlDatabase & QSqlQuery?

I got confused with the manual , should i work like this:

{
 QSqlDatabase db = QSqlDatabase::addDatabase (...);
 QSqlQuery query (db);
 query.exec (...);
}

QSqlDatabase::removeDatabase (...);

As the document points out, query or db will be deconstructed automatically. But is that efficient ?

Well , if i cache db inside a class , like the following:

class Dummy {
  Dummy() { 
    db = QSqlDatabase::addDatabase (...);
  }
  ~Dummy() {
    db.close();
  }

  bool run() {
    QSqlQuery query (db);
    bool retval = query.exec (...);
    blabla ...
  }

  private:
    QSqlDatabase db;
};

Sometimes i could see warnings like:

QSqlDatabasePrivate::removeDatabase: connection 'BLABLA' is still in use, all queries will cease to work.

Even if i didn't call run().


Source: (StackOverflow)

Qt: resizing a QLabel, containing a QPixmap, while keeping it's aspect ratio

I use a QLabel to display the content of a bigger, dynamically changing QPixmap to the user. It would be nice to make this label smaller/larger depending on the space available. The Screensize is not always as big as the QPixmap.

How can I modify the QSizePolicy and sizeHint() of the QLabel to resize the QPixmap while keeping the aspect ratio of the original QPixmap?

I can't modify sizeHint() of the QLabel, setting the minimumSize() so zero does not help. Setting "hasScaledContents()" of the QLabel allows growing, but breaking the aspect ratio thingy...

Subclassing QLabel did help, but this solution adds to much code for just a simple problem...

Any smart hints how to accompllish this without subclassing? Thanks a lot!


Source: (StackOverflow)

PyQT open a web page with JS alert pop up will get SegFault. How to fix that?

I am learning PyQT programing, and when I try a simple test, I get Segmentation fault, here is my code pop.py:

#! /usr/bin/python

import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

app = QApplication(sys.argv)
v = QWebView()
v.load(QUrl("http://127.0.0.1/j.html"))
v.show()
app.exec_()

I started an Apache server at 127.0.0.1 for testing. And here is j.html:

<html><script>alert("I am here");</script>
<body>Hello World</body>
</html>

I start the pop.py, open a window, javascript popup alert dialog, I click the OK, then pop.py will quite and get "Segmentation fault"

I tried PySide, get same result. If not JS alert in html, will be OK. Is this a bug of QT or I missed something?

I worked on Debian with python 2.6.6, python-qt4 4.8.3, libqtwebkit4 2.1.0

I also tried Fedora 15 with PyQt4-4.8.3, python 2.7.1, same issue

Any suggestion, clue for searching will be helpful. Thanks


Source: (StackOverflow)

How can I create a HTTP POST request with Qt 4.6.1?

How can I create a HTTP POST request with some URL encoded parameters using Qt 4.6.1?

I figured out that I can create a QNetworkRequest, set all the parameters there and send it via QNetworkAccessManagers post method. But how can I add some URL-encoded parameters to the request?

In the end I want to access the Eve API using Qt/C++. A Python example can be found here: http://www.eveonline.com/api/doc/example-python.asp


I managed it using something like (still to be refactored and formed into something useful):

QNetworkReply *requestApi(QNetworkAccessManager &nwam)
{

    QNetworkRequest request(QUrl("http://api.eve-online.com/account/Characters.xml.aspx"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");

    QByteArray data;
    QUrl params;

    params.addQueryItem("userid","user");
    params.addQueryItem("apiKey","key");
    data.append(params.toString());
    data.remove(0,1);

    QNetworkReply *reply = nwam.post(request,data);
    return reply;
}

Source: (StackOverflow)

Setting application info in a Qt executable file on Windows

Anyone have an tips on setting the application info (ie. right click on .exe->properties) from Qt?

I can add arbitrary version strings to Qt resource file (qrc) and display them. But most Windows installers check the version number and I can't find a Qt way of setting these fields other than manually maintaining a separate .RC file

Some way that lets you update this from an automated build would also be nice!


Source: (StackOverflow)

Qt How to make a column in QTableWidget read only

I would like to have one column in QTableWidget NOT editable.
In forums I have read a lot about some flags but could not manage to implement.

Thanks in advance!


Source: (StackOverflow)

Triple inheritance causes metaclass conflict... Sometimes

Looks like I stumbled upon a metaclass hell even when I didn't wanted anything to do with it.

I'm writing an app in Qt4 using PySide. I want to separate event-driven part from UI definition, which is generated from Qt Designer files. Hence I create a "controller" classes, but to ease my life I multiple-inherit them anyways. An example:

class BaseController(QObject):
    def setupEvents(self, parent):
        self.window = parent

class MainController(BaseController):
    pass

class MainWindow(QMainWindow, Ui_MainWindow, MainController):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setupUi(self)
        self.setupEvents(self)

This works as expected. It also has inheritance from (QDialog, Ui_Dialog, BaseController). But when I subclass BaseController and try to inherit from said subclass (in place of BaseController), I receive an error:

TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Clarification: Both QMainWindow and QDialog inherit from QObject. BaseController must also inherit from it because of Qt event system peculiarities. Ui_ classes only inherit from simple Python object class. I searched for solutions, but all of them involve cases of intentionally using metaclasses. So I must be doing something terribly wrong.

EDIT: My description may be clearer by adding graphs.

Working example:

QObject
|      \___________________
|            object        |
QMainWindow     |          BaseController
|      /---Ui_MainWindow   |
|      |                   MainController
MainWindow-----------------/

Another working example:

QObject
|      \___________________
|            object        |
QDialog         |          BaseController
|      /---Ui_OtherWindow  |
|      |                   |
OtherWindow----------------/

Not working example:

QObject
|      \___________________
|            object        |
QDialog         |          BaseController
|      /---Ui_OtherWindow  |
|      |                   OtherController
OtherWindow----------------/

Source: (StackOverflow)