Showing posts with label Qt. Show all posts
Showing posts with label Qt. Show all posts

Find file in folder recursive with Qt.


This function returns the absolute path of a file in a folder (or subfolder).
It searchs recursively in all subfolders until it finds the file or return an empty QString if the file is not found.


QString findFile( QString dir, QString fileName )
{
    QDir pathDir( dir );
    for( QFileInfo fileInfo : pathDir.entryInfoList() )
    {
        if( fileInfo.isFile() )
        {
            if( fileInfo.fileName() == fileName ) 
                return fileInfo.absoluteFilePath();
        }
        else if( !fileInfo.fileName().endsWith(".") )
        {
            QString found = findFile( fileInfo.absoluteFilePath(), fileName );
            if( !found.isEmpty() ) return found;
        }
    }
    return "";
}



Connect QObject signal to non QObject class.

Usually you need to derive from QObject and use the Q_OBJECT macro to connect to some slot in your class.
But sometimes you want to connect a QObject signal to do something in a non QObject class.

There are several posible reasons to not derive from QObject or use the Q_OBJECT macro, but in any case you can connect to a non QObejct class using lambdas:

```c
  // Connect to menu action

  QMenu* menu:

  QAction* myAction = menu->addAction( QIcon(":/myIcon.svg"), "Do something" );

  QObject::connect( myAction, &QAction::triggered, [=](){ myFuncion(); });



  // Connect to QPushButton

  QPushButton* button = new QPushButton();

  QObject::connect( button, &QPushButton::released, [=](){ onButtonReleased(); });
```


Submenu in a QMenu.

Adding a submenu to a QMenu is really easy, and you add nested submenus if you want:
```c
	QMenu* mainMenu;
	
	QAction* action1 = mainMenu->addAction(QIcon(":/icon1.png"),"Entry1" );
    connect( action1, &QAction::triggered,
                this, &MyClasss::mySlot1, Qt::UniqueConnection );

    QAction* action2 = mainMenu->addAction(QIcon(":/icon2.png"), "Entry2" );
    connect( action2, &QAction::triggered,
                this, &MyClasss::mySlot2, Qt::UniqueConnection );

	// --- SUBMENU --------------------------------------------------------
	
	QMenu* subMenu = mainMenu->addMenu( QIcon(":/menuIcon.png"), "SubMenu Title" );
	
	QAction* subAction1 = mainMenu->addAction(QIcon(":/subIcon1.png"),"subEntry1" );
    connect( subAction1, &QAction::triggered,
                   this, &MyClasss::subSlot1, Qt::UniqueConnection );

    QAction* subAction2 = mainMenu->addAction(QIcon(":/subIcon2.png"), "subEntry2" );
    connect( subAction2, &QAction::triggered,
                   this, &MyClasss::subSlot2, Qt::UniqueConnection );

	// --------------------------------------------------------------------
	
	
	QAction* action3 = mainMenu->addAction(QIcon(":/icon3.png"),"Entry3" );
    connect( action3, &QAction::triggered,
                this, &MyClasss::mySlot3, Qt::UniqueConnection );

    QAction* action4 = mainMenu->addAction(QIcon(":/icon4.png"), "Entry4" );
    connect( action4, &QAction::triggered,
                this, &MyClasss::mySlot4, Qt::UniqueConnection );
```

Integer to any base QString

This function takes an integer and returns a QString with the representation in any base < 16.

It uses the val2hex function from previous post:
.....................................................................................................
```c
QString val2hex( int d )
{
    QString Hex="0123456789ABCDEF";

    QString h = Hex.mid( d&15, 1 );

    while( d>15 )
    {
        d >>= 4;
        h = Hex.mid( d&15,1 ) + h;
    }
    return h;
}

QString decToBase( int value, int base, int digits )
{
    QString converted = "";

    for( int i=0; i= base ) converted = val2hex(value%base) + converted;
        else                converted = val2hex(value) + converted;

        value = floor( value/base );
    }
    return converted;
}
```
.....................................................................................................

Integer to Hex QString

This function takes an integer and returns a QString with the exadecimal representation:
.....................................................................................................
```c
QString val2hex( int d )
{
    QString Hex="0123456789ABCDEF";

    QString h = Hex.mid( d&15, 1 );

    while( d>15 )
    {
        d >>= 4;
        h = Hex.mid( d&15,1 ) + h;
    }
    return h;
}
```
.....................................................................................................

File to QString

Read a file and parse whole content to a QString:
.....................................................................................................
```c
QString fileToString( const QString &fileName, const QString &caller )
{
    QFile file( fileName );

    if( !file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::warning( 0l, caller, 
                              "Cannot read file "
                              +fileName+":\n"
                              +file.errorString());
        return "";
    }

    QTextStream in( &file );
    QString text = in.readAll();
    file.close();

    return text;
}
```
.....................................................................................................

File to QStringList

Read a file and parse each line to a QStringList:

.....................................................................................................

```c
QStringList fileToStringList( const QString &fileName, const QString &caller )
{
    QStringList text;
    QFile file(fileName);

    if( !file.open( QFile::ReadOnly | QFile::Text) )
    {
        QMessageBox::warning( 0l, caller, 
                              "Cannot read file "
                              +fileName+":\n"
                              +file.errorString());
        return text;
    }

    QTextStream in( &file );
    while( !in.atEnd() ) text.append( in.readLine() );
    file.close();

    return text;
}
```

.....................................................................................................

Add entry to QListWidget and resize to content

Sometimes is useful resizing a QListWidget to the size of its content.
This is one way to do it.

Don't forget resizing again when you remove an item.

.....................................................................................................

```c
void addEntry( QString name, QListWidget* list )
{
    QListWidgetItem* item = new QListWidgetItem( name, list, 0 );
  
    QFont font;
    font.setPointSize(10);
    font.setWeight(70);
    item->setFont( font );

    item->setIcon( QIcon(":/icon.png") );
       
    int iconSize = 22;                       // I know icon size

    int size = list->count()*iconSize + 2;

    list->setFixedHeight( size );
}
```

.....................................................................................................

Fast linked list without list

If you need a very fast way to iterate over a list of objects and call the same method on all of them, then here is a posible solution. Der...