Question

I have a class that uses a vertical QSplitter to separate two important widgets (the arrow pointer is at the splitter position):

enter image description here

The upper child widget is a QTextEdit while the lower child is a custom widget that interacts with my app's tagging system. The lower child is collapsed by default; it is only displayed when the user clicks the tag toolbar button or drags the splitter upward. When the button is toggled on, the lower child is displayed, otherwise it remains hidden.

The toolbar button works perfectly but right now I have no way to detect when the user collapses the lower child by dragging the splitter bar all the way down. This causes the toolbar button to stay toggled on when the user collapses the lower child by hand. The inverse is also true; when the user drags the splitter up the button stays off.

I've already tried connecting the QSplitter:splitterMoved(int pos,int index) signal to a slot that un-toggles the button when the lower child (index 1) is collapsed (pos 0) manually but for some reason the signal never gets emitted.

My code (the splitter object is called divide) connects this signal...

connect(divide, SIGNAL(splitterMoved(int,int)), this, SLOT(splitterMoved(int pos, int index)));

... to this slot:

void Editor::splitterMoved(int pos, int index){

    using namespace std;

    if((index==1) && (pos==0)){
      ui->TagButton->setChecked(false);
    }
    else{
      ui->TagButton->setChecked(true);
    }
}

Am I using this incorrectly? The slot currently does nothing no matter what I do to the splitter. Is there a better way to solve this problem?

Was it helpful?

Solution

Are you sure parameter names are allowed in the SLOT macro? A quick test suggests they're not.

Try with:

connect(divide, SIGNAL(splitterMoved(int,int)),
        this, SLOT(splitterMoved(int,int)));

UPDATE: Another point is that pos is not 0 when the child widget #1 is collapsed, on the contrary it reaches its maximum value, since it is the distance from the top. Testing QSplitter:sizes() would be easier.

Example, assuming divide is a class member:

void Editor::splitterMoved(int pos, int index){
    if(divide->sizes().at(1)==0) {
      ui->TagButton->setChecked(false);
    }
    else{
      ui->TagButton->setChecked(true);
    }

OTHER TIPS

In the case of QSplitters you can check a child widget's invisibility using:

QWidget::visibleRegion().isEmpty()

Unlike checking whether the handle is at zero this works for both extremes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top