Shift two tabs to the right in Qt, and keep the others on the left side

I am currently working on a layout design for my QtTabWidget and need to move two specific tabs to the right while keeping the rest of them aligned to the left.

It's important to note that these tabs are static and not dynamically added during runtime.

Here is my current setup: https://i.sstatic.net/uErnj.png

And here is my desired layout: https://i.sstatic.net/GVGtu.png

I have tried utilizing CSS styling with alignment:right;, but this resulted in all tabs moving to the right instead of just the ones I wanted. I also attempted using properties like float and position:absolute;right:0;, however, none of these solutions worked as expected.

My search through the Qt documentation did not yield any results that could help me achieve the desired tab positioning.

Therefore, my question remains: How can I specifically move the "Profile" and "Settings" tabs to the right while keeping the others in their original positions?

Answer №1

It seems that achieving the desired functionality with just one QTabWidget may not be feasible. Have you considered using a QTabBar instead?

If not, here's an alternative solution involving QSpacerItem and two instances of QTabBars:

  1. Create two QTabBars with your tabs.
  2. Set up a QHBoxLayout.
  3. Add the first bar to this layout, then include a QSpacerItem with an expanding policy, followed by the second bar.
  4. You can then use signal/slot connections to show or hide widgets on the screen based on bar clicks.

The challenge lies in defaulting to the first tab on each QTabBar, which can be resolved by adding an empty tab on each:

For example, consider the following snippet of code:

_firstBar = new QTabBar(this);
_firstBar->addTab("");
// Other tab additions...
_firstBar->setDrawBase(false);
// Additional styling...

_secondBar = new QTabBar(this);
_secondBar->addTab("");
// Other tab additions...
_secondBar->setDrawBase(false);
// Additional styling...

QHBoxLayout *lay = new QHBoxLayout(ui->centralWidget);
lay->addWidget(_firstBar);
lay->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed));
lay->addWidget(_secondBar);

connect(_firstBar, &QTabBar::currentChanged, this, &MainWindow::showFirstBarWidget);
connect(_secondBar, &QTabBar::currentChanged, this, &MainWindow::showSecondBarWidget);

_firstBar->setCurrentIndex(1);

Below are the corresponding slots for handling bar interactions:

void MainWindow::showFirstBarWidget(int index)
{
    if(index == 0)
        return;

    _secondBar->setCurrentIndex(0);

    switch(index) {
        // Handle showing/hiding of QWidget based on index
    }
}

void MainWindow::showSecondBarWidget(int index)
{
    if(index == 0)
        return;

    _firstBar->setCurrentIndex(0);

    switch(index) {
        // Handle showing/hiding of QWidget based on index
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is preventing me from overloading the :: operator?

As I delved into the pages of Stanley B. Lippman's book, C++ Primer, specifically focusing on Variables and Basic Types, a peculiar symbol caught my attention - the scope operator ::. Having some prior knowledge about operator overloading, I was intr ...

Optimizing the speed and load performance of SQLite in Phonegap

As someone who is new to Android programming, I am seeking advice on how to optimize the performance of my phonegap SQLite application. Currently, it takes 30 seconds to load 150 words when I hit the refresh button. Can anyone provide tips on how to enhanc ...

Adjusting the size of the div both horizontally and vertically in Angular 5 using the mouse cursor

As a beginner in Angular 5, I am looking to achieve horizontal and vertical resizing of a div by dragging with the mouse pointer. Can someone assist me in implementing this feature? ...

Does utilizing static_cast limit the ability to access public member function?

I encountered an error message stating "error: ‘A’ is an inaccessible base of ‘B’" while trying to use static_cast in the code snippet below: template<typename Derived> class A { protected: void funA() { static_cast<Derived *> (thi ...

What is the best way to implement auto-width columns with flexbox in Bootstrap 4?

My goal was to transition from using bootstrap 3 to bootstrap 4 so that I could achieve uniform-sized divs in a row by utilizing flexbox. I acquired the precompiled files from https://getbootstrap.com/docs/4.6/getting-started/download/. After downloading ...

How can the adapter pattern be effectively utilized in C++ programming?

Recently, I've come across the concept of utilizing inheritance in the following manner: class Animal { }; class Robot{ }; class Cyborg : public Animal, private Robot { }; However, I find it somewhat unconventional to inherit publicly from one cla ...

Transforming from Parent Class to Created Instance Template

I currently have multiple objects that are instances of a Class Template that need to be stored in a std::vector. To achieve this, I decided to have the Class Template inherit from a Base Class: class BaseValue { public: virtual void Test() { std: ...

The compiler mistakenly invokes the copy operator rather than the move operator

Before coming here, I did search for a similar question, but the issue I'm facing seems different. In my AnimatedSprite class, I have a member named Timer, which is intentionally made non-copyable (the copy constructor and assignment operator are expl ...

Breaking columns in Firefox using CSS

I am currently exploring a cross-browser approach to create a columned layout for an unordered list. The content is generated by a CMS, so my options are limited to adding classes only. While I have been successful in creating columns, I also need to contr ...

Modify the c3 axis labels using CSS

I've been using the c3 package in R, which serves as a wrapper for the C3 javascript charting library created by Masayuki Tanaka. One issue I encountered is that my c3 chart was cutting off the last date on the x-axis. After inspecting it in Chrome I ...

Show or conceal advertisement based on user role

I have developed a custom Web Control that acts as a container for a mobile banner, but I want to hide it from users who are admins. So far, I've attempted the following: <mob:MobileBanner runat="server" ID="MobileBanner" Visible='<%# Not ...

Width of element while applying the transform: translate()

I am currently using absolute positioning to align a ul list at the bottom center of a container. However, I have noticed that when the window or container is resized, the list seems to shrink as if there is some sort of margin, padding, or max-width appli ...

In React js, I wanted to display the animation specifically on the "add to bag" button for the added item

When I click the "add to bag" button, all other buttons also display the animation. How can I make sure that only the clicked button shows the animation? Any suggestions? <Table responsive> <thead> <tr> ...

Getting Your Content Centered Horizontally with Fixed Positioning in CSS

I would like the div to be centered horizontally with a fixed position, but it doesn't seem to work in my code. Can someone help me troubleshoot? Check out the demo here. HTML: <div id="fancybox-thumbs" class="bottom" style="width: 675px;"> ...

What is the reason for nth-of-type selectors not functioning on the third element?

I have a question regarding CSS nth-of-type. In the following code snippet, nth-of-type(2) and nth-of-type(3) are functioning properly, however, nth-of-type(4) and nth-of-type(5) are not working as expected. Could there be an issue with my code? <div i ...

Can an element be targeted for hover in CSS without it being a child element?

In my HTML code, I have an <aside> and a <header>. The <header> contains a child element called container, which has some children including one named burger bar. What I am trying to achieve is that when I hover over the burger bar elemen ...

Looking to streamline this intricate grid/table structure with the help of Twitter Bootstrap

I've been experimenting with creating a complex layout using tables in Twitter Bootstrap, but I'm struggling to get it just right – especially when it comes to displaying the number 5! Is there a way to achieve the same layout using divs instea ...

Calculating the mean of a varying range of values in a two-dimensional array using C++

I have come across similar questions on here, but none of the answers have really helped me with my specific issue: I am working with an array containing 512x512 pixels, each having a value like 165.88009. (I will eventually be creating a heatmap in Gn ...

Having trouble horizontally centering a div with a fixed position?

Is there a way to center a div horizontally inside the browser window while maintaining its vertical position? Below is the div in question: <div id="divErrorMessage" class="ErrorClientSide"> <div class="ui-icon ui-icon-info" style="float: le ...

Tips for preserving the dimensions of a container even after adding text dynamically

I have created a form and implemented a JavaScript warning for when the entered passwords do not match. However, I am facing an issue where adding the warning text causes other elements in the container to shift. Is there a way to prevent this from happe ...