Web Analytics Made Easy - Statcounter
Skip to content

Class SBGWidgetFileSelector#

ClassList > SBGWidgetFileSelector

The SBGWidgetFileSelector class is a convenience widget for specifying a file path - it contains a label, a line edit, and a push button to browse for a file.More...

  • #include <SBGWidgetFileSelector.hpp>

Inherits the following classes: QWidget

Public Slots#

Type Name
slot void onBrowse
Opens a file dialog to let the user select a file.

Public Signals#

Type Name
signal void pathChanged

Public Functions#

Type Name
SBGWidgetFileSelector (QWidget * parent=nullptr)
Constructs a path selector widget.
QString getDefaultDirectory () const
Returns the default directory of the selector.
QString getFilePath ()
Returns the path.
QString getFilter () const
Returns the filter of the selector.
QString getLabel () const
Returns the label shown in the selector.
void setDefaultDirectory (const QString & dir)
Sets the default directory for the selector that will be used for browse if the file path is not set.
void setFilePath (const QString & path)
Sets the path.
void setFilter (const QString & filter)
Sets the filter for the selector.
void setLabel (const QString & label)
Sets the label shown in the selector.
virtual ~SBGWidgetFileSelector ()
Destructs the path selector widget.

Public Static Functions#

Type Name
bool checkFile (const QString & path, bool askUser)
Check whether the file exists.

Detailed Description#

Short name: SBFileSelector

Public Slots Documentation#

slot onBrowse#

Opens a file dialog to let the user select a file.

void SBGWidgetFileSelector::onBrowse;

Determines the initial directory based on the current file path or the default directory, then invokes SBGWindowDialog::getFileNameFromUser. If the user selects a file, updates the line edit and emits pathChanged() if the path changed.


Public Signals Documentation#

signal pathChanged#

void SBGWidgetFileSelector::pathChanged;

Public Functions Documentation#

function SBGWidgetFileSelector#

Constructs a path selector widget.

SBGWidgetFileSelector::SBGWidgetFileSelector (
    QWidget * parent=nullptr
) 

When the user clicks on the browse button of this widget, it internally invokes the SBGWindowDialog::getFileNameFromUser function.

The file dialog's working directory is set to the directory of the file path, if specified, else it is set to the default directory, if specified.

// create the file selector widget
SBFileSelector* fileSelector = new SBFileSelector();
fileSelector->setLabel("Choose input file");
// add the file selector widget into a layout
// ...

You can specify the filter in the same way it is specified in QFileDialog. Only files that match the given filter are shown.

You can provide a single filter, for example:

fileSelector->setFilter("PDB file (*.pdb)");

If you want multiple filters, separate them with ';;', for example:

fileSelector->setFilter("Image files (*.png *.xpm *.jpg);;Text files (*.txt)");

See also: SAMSON::getFileNameFromUser, SAMSON::getFileNamesFromUser, SAMSON::getSaveFileNameFromUser Constructs a file selector widget.

Initializes the widget with an optional parent widget. Sets up the internal layout, group box, line edit, and browse button.

Parameters:

  • parent Optional parent widget.

function getDefaultDirectory#

Returns the default directory of the selector.

QString SBGWidgetFileSelector::getDefaultDirectory () const

Returns the default directory used for browsing when the file path is not set.

Returns:

The default directory path.


function getFilePath#

Returns the path.

QString SBGWidgetFileSelector::getFilePath () 

Returns the currently selected file path.

Returns:

The file path string from the line edit.


function getFilter#

Returns the filter of the selector.

QString SBGWidgetFileSelector::getFilter () const

Returns the filter string used by the file selector.

Returns:

The current filter expression.


function getLabel#

Returns the label shown in the selector.

QString SBGWidgetFileSelector::getLabel () const

Returns the label shown in the selector's group box.

Returns:

The current label text.


function setDefaultDirectory#

Sets the default directory for the selector that will be used for browse if the file path is not set.

void SBGWidgetFileSelector::setDefaultDirectory (
    const QString & dir
) 

Sets the default directory for the file selector.

The directory will be used as the starting point for the file dialog if the current file path is empty.

Parameters:

  • dir The default directory path.

function setFilePath#

Sets the path.

void SBGWidgetFileSelector::setFilePath (
    const QString & path
) 

Sets the file path displayed in the line edit.

Updates the line edit with the trimmed path and emits pathChanged() if the new path differs from the previous one.

Parameters:

  • path The new file path.

function setFilter#

Sets the filter for the selector.

void SBGWidgetFileSelector::setFilter (
    const QString & filter
) 

Sets the filter string for the file selector.

Parameters:

  • filter The filter expression, e.g., "PDB file (*.pdb)".

function setLabel#

Sets the label shown in the selector.

void SBGWidgetFileSelector::setLabel (
    const QString & label
) 

Sets the label shown in the selector's group box.

Parameters:

  • label The new label text.

function ~SBGWidgetFileSelector#

Destructs the path selector widget.

virtual SBGWidgetFileSelector::~SBGWidgetFileSelector () 

Destroys the file selector widget.

Releases any allocated resources associated with the widget.


Public Static Functions Documentation#

function checkFile#

Check whether the file exists.

static bool SBGWidgetFileSelector::checkFile (
    const QString & path,
    bool askUser
) 

Checks whether the specified file exists.

If the file does not exist and askUser is true, an error dialog is shown to the user.

Parameters:

  • path Path to the file to check.
  • askUser If true, display an error dialog when the file does not exist.

Returns:

true if the file exists, otherwise false.